-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Description
Sometimes, I have to use this syntax :
User::whereRaw(array('programs_ids.'.$program_id => array('$exists' => true)));
It could be better if it was :
User::whereExists('programs_ids.'.$program_id);
But a whereExists()
method already exists in Laravel. It corresponds to the WHERE EXISTS
SQL operator, wich have no sens in the MongodDB context, am I right ? It could be good to overwrite the initial method with an other one, matching the $exists
MongoDB operator like in the case above but it's impossible because the arguments are not the same (the initial method need a Closure as first argument).
We could have a whereFieldExists()
method with an implementation like that :
public function whereFieldExists($column, $exists = true, $boolean = 'and')
{
return $this->whereRaw(array($column => array('$exists' => $exists)), array(), $boolean);
}
public function whereFieldNotExists($column, $boolean = 'and')
{
return $this->whereFieldExists($column, false, $boolean);
}
After tests and writing this I'm not completely sure that this syntax is really great... What do you think about ? this question is more general because we could have also whereType
or whereAll
. I could work on this in the next days.
Activity
jenssegers commentedon Dec 7, 2013
I could just overwrite the
whereExists
method, or does this mean something different in MySQL?alexandre-butynski commentedon Dec 8, 2013
Yes, it means something different (http://dev.mysql.com/doc/refman/4.1/en/exists-and-not-exists-subqueries.html) and a method already exists. We can't override it because the first argument has to be a
Closure
.jenssegers commentedon Dec 8, 2013
And I guess
whereNotNull
does not give the same results.alexandre-butynski commentedon Dec 8, 2013
In fact,
whereNotNull
give something very close that{$exists: true}
. The only difference is were an index exists but has anull
value... but it doesn't matter in my use case. It look like a side effect but, in my particular case, it does the job for the moment. Thanks to lead me to this solution.In a more general discussion, what do you think about writing custom methods for specific MongoDB operators (
$type
,$exists
,$all
,$size
...) ?jenssegers commentedon Dec 8, 2013
I will take a look at it, should not be that hard I guess.
alexandre-butynski commentedon Dec 8, 2013
Ok, ask if you need help.
jenssegers commentedon Dec 8, 2013
Check out https://github.com/jenssegers/Laravel-MongoDB#mongodb-specific-operators
alexandre-butynski commentedon Dec 9, 2013
Great !
NominationP commentedon Aug 19, 2018
How to solve this
NominationP commentedon Aug 19, 2018
$query->whereExists(function($que){
$que->select(1)->from('bargain_coupons')->whereRaw('bargain_coupons.shop_id=id');
});
--! help
Adding mongodb specific operators, fixes mongodb#82 and mongodb#81