Soft delete doesn't work with like? |
$q = 'test';
$list = ['title' => $q, 'slug' => $q, 'body' => $q]; $data = [ 'news' => $model->orLike($list)->paginate(5), 'pager' => $model->pager ]; Using ($useSoftDeletes = true) in the model the above request is looking for deleted too. Is this correct? I think I shouldn't look for the deleted ones. Only when using withDeleted().
Model->paginate() calls Model->findAll() which respects soft deletes, so if you're finding that not to be true then it is likely a bug. Try working up a more complete example of the issue and post it to GitHub Issues as a Bug Report.
I don't think it's a bug, it's a misunderstanding about which methods take soft deletes into consideration and which class certain methods actually belong to.
A call like PHP Code: $model->where("body", $q); Looks like the Model class has a method called where. It does not. What is really being called is a Builder instance. On the other hand PHP Code: $model->find($id); actually is making a call to a method in the Model class. It is only a short list of Model methods that consider soft delete status. In terms of read operations it's find(), findAll(), and first() and that is all. There are no Builder methods that consider soft deletes.
Sorry if I'm wrong with something. : /
PHP Code: $list = ['title' => $q, 'slug' => $q, 'body' => $q]; Using orLike() with paginate() generate the sql below: But shows deleted. Code: SELECT COUNT(*) AS `numrows` In parentheses, it would work well. ![]() WHERE (`title` LIKE '%teste%' ESCAPE '!' OR `slug` LIKE '%teste%' ESCAPE '!' OR `body` LIKE '%teste%' ESCAPE '!') AND (`news`.`deleted_at` IS NULL)
(11-02-2019, 06:40 AM)edica Wrote: Sorry if I'm wrong with something. : / paginate() is another Model method. Internally it uses Model::findAll(), so it's not smart enough to put a parenthesis where it might be needed. You can use groupStart() and groupEnd() to do that yourself in some circumstances. But I'm not sure it will work as desired when mixing Model ( find() ) and Builder ( orLike() )methods.
It worked:
$model->groupStart()->orLike($list)->groupEnd()->paginate(5) The CI4 CRUD for now is simple. So would it be better to use only the Query Builder Class?
(11-02-2019, 10:57 AM)edica Wrote: So would it be better to use only the Query Builder Class? I think the idea was to have a few Model methods for some very frequently needed tasks related to basic CRUD operations. I don't know that these were intended to "look" or fully behave like Builder functions for implementing more complex CRUD ops. Looks like you're able to make it work so that's a good thing. If you can make it do what you need then why not use it? While we're talking about Model vs Builder, it might be smart to keep in mind that the Model's "write" functions (insert, update, etc) are more than simple wrappers around Builder methods of the same name, nor are they using __call() "magic" to get to Builder methods. So make sure you understand what the core code is doing before trying to chain Model methods with Builder methods. |
Welcome Guest, Not a member yet? Register Sign In |