matching() public method

This function will add entries in the contain graph. ### Example: Bring only articles that were tagged with 'cake' $query->matching('Tags', function ($q) { return $q->where(['name' => 'cake']); ); It is possible to filter by deep associations by using dot notation: ### Example: Bring only articles that were commented by 'markstory' $query->matching('Comments.Users', function ($q) { return $q->where(['username' => 'markstory']); ); As this function will create INNER JOIN, you might want to consider calling distinct on this query as you might get duplicate rows if your conditions don't filter them already. This might be the case, for example, of the same user commenting more than once in the same article. ### Example: Bring unique articles that were commented by 'markstory' $query->distinct(['Articles.id']) ->matching('Comments.Users', function ($q) { return $q->where(['username' => 'markstory']); ); Please note that the query passed to the closure will only accept calling select, where, andWhere and orWhere on it. If you wish to add more complex clauses you can do it directly in the main query.
public matching ( string $assoc, callable $builder = null )
$assoc string The association to filter by
$builder callable a function that will receive a pre-made query object that can be used to add custom conditions or selecting some fields
Example #1
1
 public function findWithCount(Query $query, array $options)
 {
     $query->select(['count' => $query->func()->count('Users.id')]);
     $query->matching('Users');
     $query->group(['UserRoles.id']);
     return $query;
 }
 public function findWithCitiesBiggerThanDenmark(Query $query)
 {
     $denmarkPopulation = $this->find()->select(['population'])->where(['id' => 'DNK']);
     return $query->matching('Cities', function ($q) use($denmarkPopulation) {
         return $q->where(['Cities.population >' => $denmarkPopulation]);
     });
 }
Example #3
0
 /**
  * "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 findTagged(Query $query, array $options)
 {
     $query->contain(['Users', 'Users.AccountParameters', 'Hashtags']);
     $query->matching('Hashtags', function ($q) use($options) {
         return $q->where(['Hashtags.name' => $options['tag_name']]);
     });
     $query->order(['Tweets.created' => 'DESC']);
     return $query;
 }
 public function findCityProbability(Query $query)
 {
     return $query->matching('Countries.Cities', function ($q) {
         $prob = $q->newExpr('(Languages.percentage / 100) * (Cities.population / Countries.population)');
         return $q->select(['probability' => $prob, 'Cities.name'])->where(function ($exp) use($prob) {
             return $exp->gte($prob, 0.25);
         });
     });
 }
Example #6
0
 /**
  * Dynamic finder that find threads where given users are involved
  *
  * @param \Cake\ORM\Query $query the original query to append to
  * @param array $users the list of user ids like ```[1, 2, 3]```
  * @return \Cake\ORM\Query The amended query
  */
 public function findParticipating(Query $query, array $users = null)
 {
     if (empty($users)) {
         return $query;
     }
     return $query->matching('Users', function ($q) use($users) {
         return $q->where(['Users.id IN' => $users]);
     });
 }
Example #7
0
 /**
  * {@inheritDoc}
  */
 public function scope(Query $query, TokenInterface $token)
 {
     $value = $token->value();
     if (empty($value)) {
         return $query;
     }
     return $query->matching('Tags', function ($q) use($value) {
         $value = explode(',', $value);
         $names = array_map('trim', (array) $value);
         return $q->where(['Tags.name IN' => empty($names) ? ['---'] : $names]);
     });
 }
Example #8
0
 /**
  * Authors search finder
  *
  * @param  Query $query query object instance
  * @return $this
  */
 public function findWithAuthors(Query $query)
 {
     return $query->matching('Authors');
 }
Example #9
0
 public function findForMembers(Query $query, array $options)
 {
     return $query->matching('Users.Memberships', function ($q) {
         return $q->where(['Memberships.expires >=' => date('Y-m-d H:i:s')])->where([function ($exp, $q) {
             return $exp->isNull('canceled');
         }]);
     })->distinct(['Tags.id']);
 }
Example #10
0
 public function findByHashtag(Query $query, array $options)
 {
     return $query->matching('Hashtags', function ($q) use($options) {
         return $q->where(['Hashtags.name' => $options['hashtag']]);
     });
 }
 public function findTagged(Query $query, $options)
 {
     return $query->matching('Tags', function ($q) use($options) {
         return $q->where(['Tags.title' => $options['tag']]);
     });
 }
 public function findVencido(Query $query, array $options)
 {
     return $query->matching('Anuncio', function ($q) {
         return $q->find('vencido');
     });
 }