CodeIgniter Forums
Soft delete doesn't work with like? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forum-31.html)
+--- Thread: Soft delete doesn't work with like? (/thread-74720.html)



Soft delete doesn't work with like? - edica - 10-29-2019

$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().


RE: Soft delete doesn't work with like? - MGatner - 11-01-2019

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.


RE: Soft delete doesn't work with like? - edica - 11-01-2019

It could be a bug. https://github.com/codeigniter4/CodeIgniter4/issues/2380


RE: Soft delete doesn't work with like? - dave friend - 11-01-2019

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.


RE: Soft delete doesn't work with like? - edica - 11-02-2019

Sorry if I'm wrong with something. : /


PHP Code:
$list = ['title' => $q'slug' => $q'body' => $q];
$model->orLike($list)->paginate(5


Using orLike() with paginate() generate the sql below:
But shows deleted.


Code:
SELECT COUNT(*) AS `numrows`
FROM `news`
WHERE `title` LIKE '%test%' ESCAPE '!'
OR  `slug` LIKE '%test%' ESCAPE '!'
OR  `body` LIKE '%test%' ESCAPE '!'
AND `news`.`deleted_at` IS NULL


In parentheses, it would work well.  Undecided

WHERE (`title` LIKE '%teste%' ESCAPE '!'
OR  `slug` LIKE '%teste%' ESCAPE '!'
OR  `body` LIKE '%teste%' ESCAPE '!')
AND (`news`.`deleted_at` IS NULL)


RE: Soft delete doesn't work with like? - dave friend - 11-02-2019

(11-02-2019, 06:40 AM)edica Wrote: Sorry if I'm wrong with something. : /


PHP Code:
$list = ['title' => $q'slug' => $q'body' => $q];
$model->orLike($list)->paginate(5


Using orLike() with paginate() generate the sql below:
But shows deleted.


Code:
SELECT COUNT(*) AS `numrows`
FROM `news`
WHERE `title` LIKE '%test%' ESCAPE '!'
OR  `slug` LIKE '%test%' ESCAPE '!'
OR  `body` LIKE '%test%' ESCAPE '!'
AND `news`.`deleted_at` IS NULL


In parentheses, it would work well.  Undecided

WHERE (`title` LIKE '%teste%' ESCAPE '!'
OR  `slug` LIKE '%teste%' ESCAPE '!'
OR  `body` LIKE '%teste%' ESCAPE '!')
AND (`news`.`deleted_at` IS NULL)

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.


RE: Soft delete doesn't work with like? - edica - 11-02-2019

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?


RE: Soft delete doesn't work with like? - dave friend - 11-02-2019

(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.