Issue
I have two custom find methods and I would like to make an union of them.
public function findInGroup(Query $query, array $options)
{
return $query
->matching(
'Groups',
function ($q) {
return $q->where(['Groups.id' => 1]);
}
);
}
public function findAsUser(Query $query, array $options)
{
return $query
->matching(
'Users',
function ($q) {
return $q->where(['Users.id' => 1]);
}
);
}
The two methods generates different select
list, because of the matching
call. So I can not make union of them...
I do not need Groups__id
, etc fields to be selected.
Is there any way to tell to matching
that it should not add the matching data fields to the created select field list?
Solution
You have two options"
Select all the fields from the main query
That will make the query builder omit the fields from associations:
$query
->find('iGroup', $options)
->find('asUser', $options)
->select($table->schema()->columns());
By specifically selecting the columns you need, you will leave out the columns form associations that can be joined, i.e. the columns from matching.
Use CakePHP 3.1
CakePHP 3.1 introduces a new function called innerJoinWith()
, it does exactly the same as matching()
but it will not select any columns from the association:
public function findInGroup(Query $query, array $options)
{
return $query
->innerJoinWith(
'Groups',
function ($q) {
return $q->where(['Groups.id' => 1]);
}
);
}
public function findAsUser(Query $query, array $options)
{
return $query
->innerJoinWith(
'Users',
function ($q) {
return $q->where(['Users.id' => 1]);
}
);
}
Answered By - José Lorenzo Rodríguez
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.