Skip to content

[Proposal] $query->whereFieldExists #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
alexandre-butynski opened this issue Dec 7, 2013 · 10 comments
Closed

[Proposal] $query->whereFieldExists #82

alexandre-butynski opened this issue Dec 7, 2013 · 10 comments

Comments

@alexandre-butynski
Copy link
Contributor

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.

@jenssegers
Copy link
Contributor

I could just overwrite the whereExists method, or does this mean something different in MySQL?

@alexandre-butynski
Copy link
Contributor Author

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
Copy link
Contributor

And I guess whereNotNull does not give the same results.

@alexandre-butynski
Copy link
Contributor Author

In fact, whereNotNull give something very close that {$exists: true}. The only difference is were an index exists but has a null 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
Copy link
Contributor

I will take a look at it, should not be that hard I guess.

@alexandre-butynski
Copy link
Contributor Author

Ok, ask if you need help.

@jenssegers
Copy link
Contributor

Check out https://github.com/jenssegers/Laravel-MongoDB#mongodb-specific-operators

@alexandre-butynski
Copy link
Contributor Author

Great !

@NominationP
Copy link

How to solve this

@NominationP
Copy link

NominationP commented Aug 19, 2018

$query->whereExists(function($que){
$que->select(1)->from('bargain_coupons')->whereRaw('bargain_coupons.shop_id=id');
});

--! help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants