/** * Custom finder method, returns recent to-do's based on status * * @param Query $query cakephp query object * @param array $options list of options * @return query $query cakephp query object */ public function findRecent(Query $query, array $options = ['status' => 0]) { return $query->where(['is_done' => $options['status']])->order(['updated' => 'DESC'])->formatResults(function ($results, $query) { return $results->map(function ($row) { $timeCreated = new Time($row->created); $timeUpdated = new Time($row->updated); $row->created = $timeCreated->timeAgoInWords(); $row->updated = $timeUpdated->timeAgoInWords(); $row->todo = htmlspecialchars($row->todo); return $row; }); }); }
public function findBetween(Query $query, array $options) { if (array_key_exists('start', $options)) { $query->where(['Requests.created <=' => new DateTime($options['start'])]); } if (array_key_exists('end', $options)) { $query->where(['Requests.created >=' => new DateTime($options['end'])]); } return $query; }
/** * beforeFind * @param Event $event * @param Query $query * @param ArrayObject $options * @param $primary */ public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary) { $getOptions = $query->getOptions(); if (!array_key_exists('enableSoftDelete', $getOptions) || $getOptions['enableSoftDelete'] == true) { $modelName = $this->_table->alias(); $booleanField = $this->config('boolean'); $timestampField = $this->config('timestamp'); if ($booleanField !== false && $this->_table->hasField($booleanField)) { $query->where([$modelName . '.' . $booleanField => false]); } if ($booleanField === false && $timestampField !== false && $this->_table->hasField($timestampField)) { $query->where([$modelName . '.' . $timestampField . ' IS' => null]); } } }
/** * {@inheritDoc} */ public function scope(Query $query, TokenInterface $token) { $column = $this->config('field'); $value = $token->value(); if (!$column || empty($value)) { return $query; } $tableAlias = $this->_table->alias(); $range = $this->_parseRange($token->value()); if ($range['lower'] !== $range['upper']) { $conjunction = $token->negated() ? 'AND NOT' : 'AND'; $conditions = ["{$conjunction}" => ["{$tableAlias}.{$column} >=" => $range['lower'], "{$tableAlias}.{$column} <=" => $range['upper']]]; } else { $cmp = $token->negated() ? '<=' : '>='; $conditions = ["{$tableAlias}.{$column} {$cmp}" => $range['lower']]; } if ($token->where() === 'or') { $query->orWhere($conditions); } elseif ($token->where() === 'and') { $query->andWhere($conditions); } else { $query->where($conditions); } return $query; }
/** * Returns query with applied filter * * @param \Cake\ORM\Query $query Query. * @param string $field Field name. * @param string $value Field value. * @param array $data Filters values. * @return \Cake\ORM\Query */ protected function _buildQuery(Query $query, $field, $value, array $data = []) { if (is_array($value)) { $field .= ' IN'; } return $query->where([$field => $value]); }
/** * Find panels by requestid * * @param Cake\ORM\Query $query The query * @param array $options The options to use. * @return Cake\ORM\Query The query. * @throws \RuntimeException */ public function findByRequest(Query $query, array $options) { if (empty($options['requestId'])) { throw new \RuntimeException('Missing request id in findByRequest.'); } return $query->where(['Panels.request_id' => $options['requestId']])->order(['Panels.title' => 'ASC']); }
/** * Filters Query to only show records that are marked visible * exposed to tables as `$table->find('visible')` * @param Query $query * @param array $options * @return Query */ public function findVisible(Query $query, array $options) { # get the merged (possibly customized) config values $config = $this->config(); # filter the query by visibility return $query->where(["{$this->_table->alias()}.{$config['field']}" => true]); }
/** * Returns query with applied filter * * @param \Cake\ORM\Query $query Query. * @param string $field Field name. * @param string $value Field value. * @param array $data Filters values. * @return \Cake\ORM\Query */ protected function _buildQuery(Query $query, $field, $value, array $data = []) { $value = '%' . $value . '%'; return $query->where(function ($exp) use($field, $value) { return $exp->like($field, $value); }); }
/** * Find all tasks grouped by project * * @param Query $query * @param type $options * @return Query */ public function findTasksByProject(Query $query, $options = []) { $options += ['where' => NULL]; $query->find('list', ['groupField' => 'project_id']); $query->where($options['where']); return $query; }
public function findByAuthor(Query $query, array $options = []) { if (isset($options['author_id'])) { $query->where(['Articles.id' => $options['author_id']]); } return $query; }
/** * Check * * @param \Cake\Event\Event $event The beforeFind event that was fired. * @param \Cake\ORM\Query $query Query * @param \ArrayObject $options The options for the query * @return void */ public function checkRecordAccess(Event $event, Query $query, ArrayObject $options) { $table = TableRegistry::get('RolesCapabilities.Capabilities'); // current request parameters $request = $table->getCurrentRequest(); // skip if current model does not match request's model if (array_diff(pluginSplit($event->subject()->registryAlias()), [$request['plugin'], $request['controller']])) { return; } // get capability owner type identifier $type = $table->getTypeOwner(); // get user's action capabilities $userActionCapabilities = $table->getUserActionCapabilities(); // skip if no user's action capabilities found or no user's action // owner specific capabilities found for current request's action if (empty($userActionCapabilities)) { return; } if (!isset($userActionCapabilities[$request['plugin']][$request['controller']][$request['action']][$type])) { return; } // set query where clause based on user's owner capabilities assignment fields foreach ($userActionCapabilities[$request['plugin']][$request['controller']][$request['action']][$type] as $userActionCapability) { $query->where([$userActionCapability->getField() => $table->getCurrentUser('id')]); } }
/** * forUser finder method * * It will find all todos for a given user id * * @param Query $q The Query builder * @param $options The options should have an index `id` * @return Query */ public function findForUser(Query $query, $options) { if (!isset($options['id'])) { throw new \InvalidArgumentException("You should provide an user id through \$options\\['id'\\]"); } return $query->where(['user_id' => $options['id']]); }
public function _addParametersToQuery(Query $query, $restrictTo = []) { $parameters = $this->request->query; $parameters = $this->_restrictTo($parameters, $restrictTo); $query->where($parameters); return $query; }
/** * beforeFind callback * * inject where condition if context is 'tenant' * * @param \Cake\Event\Event $event The beforeFind event that was fired. * @param \Cake\ORM\Query $query The query. * @return void */ public function beforeFind(Event $event, Query $query) { if (MTApp::getContext() == 'tenant') { $query->where([$this->_table->alias() . '.' . $this->config('foreign_key_field') . ' IN' => [$this->config('global_value'), MTApp::tenant()->id]]); } return $query; }
/** * Custom finder, used with fixture data to ensure Paginator is sending options * * @param Cake\ORM\Query $query * @param array $options * @return Cake\ORM\Query */ public function findAuthor(Query $query, array $options = []) { if (isset($options['author_id'])) { $query->where(['PaginatorPosts.author_id' => $options['author_id']]); } return $query; }
/** * "Active" find method * @param Query $query Query object * @param array $options Options * @return Query Query object */ public function findActive(Query $query, array $options) { $query->where([sprintf('%s.active', $this->alias()) => true]); $query->matching('Albums', function ($q) { return $q->where([sprintf('%s.active', $this->Albums->alias()) => true]); }); return $query; }
public function findByStatus(Query $query, array $options) { $status = $options['status']; if (ucfirst($status) === 'All') { return $query; } else { return $query->where(['status' => ucfirst($status)]); } }
/** * Injects configured fields into finder conditions. * * @param \Cake\Event\Event $event Event. * @param \Cake\ORM\Query $query Query. * @param \ArrayObject $options Options. * @return void */ public function beforeFind(Event $event, Query $query, ArrayObject $options) { $eventName = $event->name(); $config = (array) $this->config('events.' . $eventName); foreach (array_keys($config) as $field) { $path = $this->config('propertiesMap.' . $field); $query->where([$field => current(Hash::extract((array) $options, $path))]); } }
/** * Returns query with applied filter * * @param \Cake\ORM\Query $query Query. * @param string $field Field name. * @param string $value Field value. * @param array $data Filters values. * @return \Cake\ORM\Query */ protected function _buildQuery(Query $query, $field, $value, array $data = []) { // @todo bind to parent Articles.id using initialization parameter $alias = $query->repository()->alias(); $tags = TableRegistry::get('ArticlesTags')->find('all')->matching('Tags', function ($q) use($value, $alias) { return $q->where(['Tags.name' => $value]); })->where(["ArticlesTags.article_id = {$alias}.id"]); return $query->where([new UnaryExpression('EXISTS', $tags)]); }
/** * beforeFind callback * * inject where condition if context is 'tenant' * * @param \Cake\Event\Event $event The afterSave event that was fired. * @param \Cake\ORM\Query $query The query. * @return void */ public function beforeFind(Event $event, Query $query, $options) { // if context is tenant, add conditions to query if (MTApp::getContext() == 'tenant') { // check if find option has "skipTenant", recursive error fix if (!isset($options['skipTenantCheck']) || $options['skipTenantCheck'] !== true) { // secure the configured tenant table by adding a primary key condition if ($this->_table->alias() === MTApp::config('model')['className']) { $query->where([$this->_table->alias() . '.' . $this->_table->primaryKey() => MTApp::tenant()->id]); } else { $query->where([$this->_table->alias() . '.' . $this->config('foreign_key_field') => MTApp::tenant()->id]); } } } else { throw new DataScopeViolationException('Tenant Scoped accessed globally'); } return $query; }
/** * {@inheritDoc} */ public function scope(Query $query, TokenInterface $token) { list($conjunction, $value) = $this->_prepareConjunction($token); $subQuery = TableRegistry::get('Clients.ClientsRecords')->find()->contain(['Client'])->select(['ClientsRecords.record_id'])->where(['OR' => ["Client.name {$conjunction}" => $value, "Client.lastname {$conjunction}" => $value]]); $conditions = ['SearchDatasets.entity_id IN' => $subQuery]; if ($token->where() === 'or') { $query->orWhere($conditions); } elseif ($token->where() === 'and') { $query->andWhere($conditions); } return $query->where($conditions); }
public function findMeetings(Query $query, array $options) { $query->where(['id' => $options['Users.id']])->contain(['Meetings' => function ($q) use($options) { return $q->contain(['Users' => function ($q) use($options) { return $q; //->select(['id', 'name']) //->where(['Users.id !=' => $options['Users.id']]) //->autoFields(false); }])->contain(['Customers' => function ($q) use($options) { return $q; //->select(['id', 'name']) //->autoFields(false); }]); }]); return $query; }
public function beforeFind(Event $event, Query $query, $options, $primary) { $config = $this->config(); $tableAlias = $query->repository()->alias(); $founded = false; if ($where = $query->clause('where')) { $where->iterateParts(function ($w) use($config, $tableAlias, &$founded) { $field = is_object($w) ? $w->getField() : $w; if ($field == $tableAlias . '.' . $config['field'] || $field == $config['field']) { $founded = true; } return $w; }); } if (!$founded) { $query->where(['deleted' => 0]); } }
/** * {@inheritDoc} */ public function scope(Query $query, TokenInterface $token) { $column = $this->config('field'); $value = $token->value(); if (!$column || empty($value)) { return $query; } list($conjunction, $value) = $this->_prepareConjunction($token); $tableAlias = $this->_table->alias(); $conditions = ["{$tableAlias}.{$column} {$conjunction}" => $value]; if ($token->where() === 'or') { $query->orWhere($conditions); } elseif ($token->where() === 'and') { $query->andWhere($conditions); } else { $query->where($conditions); } return $query; }
/** * Handles the "term:" search operator. Which filters all entities matching * a given collection of terms. * * term:cat,dog,bird,...,term-slug * * You can provide up to 10 terms as maximum. * * @param \Cake\Event\Event $event The event that was triggered * @param \Cake\ORM\Query $query The query being scoped * @param \Search\Token $token Operator token * @return \Cake\ORM\Query Scoped query */ public function operatorTerm(Event $event, $query, $token) { $slugs = explode(',', $token->value()); $slugs = array_slice($slugs, 0, 10); if (!empty($slugs)) { $IN = $token->negated() ? 'NOT IN' : 'IN'; $table = $event->subject(); $pk = $table->primaryKey(); $tableAlias = $table->alias(); $termsIds = TableRegistry::get('Taxonomy.Terms')->find()->select(['id'])->where(['Terms.slug IN' => $slugs])->all()->extract('id')->toArray(); $termsIds = empty($termsIds) ? [0] : $termsIds; $subQuery = TableRegistry::get('Taxonomy.EntitiesTerms')->find()->select(['entity_id'])->where(['term_id IN' => $termsIds, 'table_alias' => $tableAlias]); if ($token->where() === 'or') { $query->orWhere(["{$tableAlias}.{$pk} {$IN}" => $subQuery]); } elseif ($token->where() === 'and') { $query->andWhere(["{$tableAlias}.{$pk} {$IN}" => $subQuery]); } else { $query->where(["{$tableAlias}.{$pk} {$IN}" => $subQuery]); } } return $query; }
/** * Alters the passed query so that it only returns scoped records as defined * in the tree configuration. * * @param \Cake\ORM\Query $query the Query to modify * @return \Cake\ORM\Query */ protected function _scope($query) { $config = $this->config(); if (is_array($config['scope'])) { return $query->where($config['scope']); } if (is_callable($config['scope'])) { return $config['scope']($query); } return $query; }
/** * "Active" find method * @param Query $query Query object * @param array $options Options * @return Query Query object */ public function findActive(Query $query, array $options) { $query->where([sprintf('%s.active', $this->alias()) => true, sprintf('%s.is_spot', $this->alias()) => false, sprintf('%s.created <=', $this->alias()) => new Time()]); return $query; }
/** * [_findUnpublished description] * * @param Query $query Config * @param array $options Options * @return void */ protected function _findUnpublished(Query $query, array $options) { $query->where(['published' => 'N']); return $query; }
/** * Find all active users. * * @param Query $query The query to decorate. * @return Query */ public function findActive(Query $query) { $query->where(['Users.active' => true]); return $query; }
/** * Custom finder to get all rows (`trashed` or not). * * @param \Cake\ORM\Query $query Query. * @param array $options Options. * @return \Cake\ORM\Query */ public function findWithTrashed(Query $query, array $options) { return $query->where(['OR' => [$query->newExpr()->isNotNull($this->getTrashField()), $query->newExpr()->isNull($this->getTrashField())]]); }