• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Soft delete doesn't work with like?

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

#2
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.
Reply

#3
It could be a bug. https://github.com/codeigniter4/CodeIgni...ssues/2380
Reply

#4
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.
Reply

#5
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)
Reply

#6
(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.
Reply

#7
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?
Reply

#8
(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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.