/** * Get the results of the relationship. * * @return mixed */ public function getResults() { $thisObject = $this; return zbase_cache($this->cacheKey($this), function () use($thisObject) { return $thisObject->query->get(); }, [$this->parent->getTable()]); }
/** * Execute the query as a "select" statement. * * @param array $columns * @return \Illuminate\Database\Eloquent\Collection */ public function get($columns = ['*']) { $columns = $this->query->getQuery()->columns ? [] : $columns; $thisObject = $this; return zbase_cache($this->cacheKey($this), function () use($thisObject, $columns) { $select = $thisObject->getSelectColumns($columns); $models = $thisObject->query->addSelect($select)->getModels(); $thisObject->hydratePivotRelation($models); if (count($models) > 0) { $models = $thisObject->query->eagerLoadRelations($models); } return $thisObject->related->newCollection($models); }, [$this->parent->getTable()]); }
/** * REturn the number of rows * @param array $filters * @param array $joins * @param array $unions * @param array $group * @param array $options */ public function count($filters = null, $joins = null, $unions = null, $group = null, $options = null) { $builder = $this->_query(['COUNT(1)'], $filters, null, $joins, $unions, $group, $options); $logMsg = __METHOD__ . PHP_EOL; $logMsg .= $this->getSqlStatement($builder) . PHP_EOL; $logMsg .= json_encode($this->getSqlBindings($builder)) . PHP_EOL; return zbase_cache(zbase_cache_key($this, __FUNCTION__, func_get_args(), $this->getModel()->getTable()), function () use($builder) { return $builder->count(); }, [$this->getModel()->getTable()], null, ['logFile' => 'Repo_' . $this->getModel()->getTable(), 'logMsg' => $logMsg]); }
/** * Return a User By attribute * @param type $attribute * @param type $value */ public function by($attribute, $value) { $cacheKey = zbase_cache_key(zbase_entity($this->entityName()), 'by_' . $attribute . '_' . $value); return zbase_cache($cacheKey, function () use($attribute, $value) { return $this->repo()->by($attribute, $value)->first(); }, [$this->entityName()], 60 * 24, ['forceCache' => true, 'driver' => 'file']); }
/** * Return a User By attribute * @param type $attribute * @param type $value */ public function postBy($attribute, $value) { $tableName = $this->postTableName(); $cacheKey = zbase_cache_key(zbase_entity($tableName), 'by_' . $attribute . '_' . $value); return zbase_cache($cacheKey, function () use($attribute, $value) { return $this->repo()->by($attribute, $value)->first(); }, [$tableName], $this->postCacheMinutes(), ['forceCache' => $this->postCacheForce(), 'driver' => $this->postCacheDriver()]); }
/** * Return the NotNotified notifications * These are the notification counts that are displayed * * @return Notification[] */ public function notificationsNotNotified() { $cacheKey = zbase_cache_key(zbase_entity($this->entityName), 'notifications_not_notified_' . $this->id()); return zbase_cache($cacheKey, function () { $filters = ['user_id' => ['eq' => ['field' => 'user_id', 'value' => $this->id()]], 'is_notified' => ['eq' => ['field' => 'is_notified', 'value' => 0]]]; return zbase_entity('user_notifications')->repo()->all($filters); }, [zbase_entity($this->entityName)->getTable()], 60 * 24, ['forceCache' => true, 'driver' => 'file']); }
/** * * @param string $method The property or method to be called * @param string $name The name of the relationship index * @param array $config The config of the relationship * @return HasOne|BelongsTo */ protected function _relationship($method, $name, $config) { $rType = zbase_value_get($config, 'type', false); if (!empty($rType)) { $rMethod = zbase_value_get($config, 'class.method', zbase_string_camel_case($name)); $isFetching = preg_match('/fetch/', $method); if ($isFetching) { $method = zbase_string_camel_case(str_replace('fetch', '', $method)); } if ($rMethod == $method) { $rEntity = zbase_value_get($config, 'entity', false); $rInverse = zbase_value_get($config, 'inverse', false); $model = zbase_entity($rEntity); $lKey = zbase_value_get($config, 'keys.local', null); $fKey = zbase_value_get($config, 'keys.foreign', null); if (!empty($rEntity)) { switch (strtolower($rType)) { case 'onetoone': if (!empty($rInverse)) { $relObj = new BelongsTo($model->newQuery(), $this, $model->getTable() . '.' . $fKey, $lKey, $name); } else { $relObj = new HasOne($model->newQuery(), $this, $model->getTable() . '.' . $fKey, $lKey); } break; case 'onetomany': if (!empty($rInverse)) { $relObj = new BelongsTo($model->newQuery(), $this, $model->getTable() . '.' . $fKey, $lKey, $name); } else { return new HasMany($model->newQuery(), $this, $model->getTable() . '.' . $fKey, $lKey); } break; case 'manytomany': $pivot = zbase_value_get($config, 'pivot', null); return new BelongsToMany($model->newQuery(), $this, $pivot, $fKey, $lKey, $name); case 'hasmanythrough': break; case 'morphto': break; case 'morphtomany': break; case 'belongsto': return new BelongsTo($model->newQuery(), $this, $model->getTable() . '.' . $fKey, $lKey, $name); default: } if (!empty($relObj)) { if ($this->relationshipMode == 'result') { return zbase_cache($this->cacheKey($relObj), function () use($relObj) { return $relObj->getResults(); }, [$this->getTable()]); } return $relObj; } } } } return false; }