Skip to content

Issue with Soft-delete with orWhere #310

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
luis-deguzman opened this issue Sep 2, 2014 · 4 comments
Closed

Issue with Soft-delete with orWhere #310

luis-deguzman opened this issue Sep 2, 2014 · 4 comments
Labels

Comments

@luis-deguzman
Copy link

This issue was first reported in issue #43 but was closed and not resolved.

I have additional information re the issue to see if someone can help in the resolution.

Here's the query:

$row = Feature::where('name', '=', $input['name'])->orWhere('display_name', '=', $input['display_name'])->get();

We have a sql logger and this what is showing up in our log:

features.find({"$and":[{"deleted_at":null},{"name":"sun_roofs"}],"$or":[{"display_name":"Sun Roof"}]})

I then changed the query to:

$row = Feature::orWhere('name', '=', $input['name'])->orWhere('display_name', '=', $input['display_name'])->get();

The log shows:

features.find({"$or":[{"deleted_at":null},{"name":"sun_roofs"},{"display_name":"Sun Roof"}]})

In both queries, the "deleted_at" clause is not set properly - and it is resulting in wrong results being returned.

@arcadas
Copy link

arcadas commented Feb 3, 2015

+1

@jenssegers jenssegers added the bug label Feb 3, 2015
@arcadas
Copy link

arcadas commented Feb 3, 2015

I round the problem by this not too nice solution:

Book::whereNotIn('user_id', [0])
  ->orWhere('user_id', $userId)
  ->orWhereIn('group_id', $groups)
  ->get()
book.find{{"$and":[
  {"deleted_at":null},
  {"user_id":{"$nin":[0]}}
],
"$or":[
  {"user_id":"541bf64fd4b41cda04fad053"},
  {"group_id":{"$in":[1,2]}}
]}, []}

The whereNotIn('user_id', [0]) condition always will be true.

Original (not works):

Book::where('user_id', $userId)
  ->orWhereIn('group_id', $groups)
  ->get()
book.find({"$and":[
  {"deleted_at":null},
  {"user_id":"541bf64fd4b41cda04fad053"}
],
"$or":[
  {"group_id":{"$in":[1,2]}}
]}, [])

I also tried (not works):

Book::orWhere('user_id', $userId)
  ->orWhereIn('group_id', $groups)
  ->get()
book.find({"$or":[
  {"deleted_at":null},
  {"user_id":"541bf64fd4b41cda04fad053"},
  {"group_id":{"$in":[1,2]}}
]}, [])

@jithinjose2
Copy link

+1

1 similar comment
@artemverbo
Copy link

+1

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

No branches or pull requests

5 participants