CodeIgniter Forums
->onlyDeleted() returns all - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: ->onlyDeleted() returns all (/showthread.php?tid=75804)



->onlyDeleted() returns all - niklas - 03-18-2020

Hello,

I would like to use the softDelete Function from the CI Model Class. If I call the class with
PHP Code:
->where(xyz)->delete() 
it work's finde.
In my database the datetime will be written to "deleted_at".

But as i started to implement a recycle bin to my application i want to get all items which are not null in deleted_at.

CI Documentation says:
PHP Code:
$modelclass->onlyDeleted()->findAll(); 

But this return all entries also the not deleted... Undecided

I build a function on my own to get just deleted items. It looks like this and it is also working but i think this is not the right way..
PHP Code:
$deletedPersons = array();
foreach(
$allPersons as $person)
{
        if(strtotime($person->deleted_at) > 0)
        {
              array_push($deletedPersons,$person);
        }


Does anyone have experience with soft deletes in CI4 ?
Maybe I used it the wrong way?

best regards,
Niklas


RE: ->onlyDeleted() returns all - includebeer - 03-22-2020

Can you confirm there are rows with the deleted_at field with a NULL value? Maybe your previous delete() really deleted all the rows.


RE: ->onlyDeleted() returns all - niklas - 03-25-2020

(03-22-2020, 08:08 AM)includebeer Wrote: Can you confirm there are rows with the deleted_at field with a NULL value?  Maybe your previous delete() really deleted all the rows.

Yes i can confirm that the "deleted_at" rows have values so they are soft deleted and other rows have null. Both are returned by ->onlyDeleted().

Maybe there is a problem with mySql field settings. If the datetime field is null there is a "0000-00-00 00:00" in it. I don't know how CodeIgniter checks that field.


RE: ->onlyDeleted() returns all - includebeer - 03-27-2020

(03-25-2020, 03:58 AM)niklas Wrote:
(03-22-2020, 08:08 AM)includebeer Wrote: Can you confirm there are rows with the deleted_at field with a NULL value?  Maybe your previous delete() really deleted all the rows.

Yes i can confirm that the "deleted_at" rows have values so they are soft deleted and other rows have null. Both are returned by ->onlyDeleted().

Maybe there is a problem with mySql field settings. If the datetime field is null there is a "0000-00-00 00:00" in it. I don't know how CodeIgniter checks that field.

If there is a value of “0000-00-00 00:00”, then it is not NULL.
Is the deleted_at column nullable in your MySQL table? Maybe it have a default value of “0000-00-00 00:00”?


RE: ->onlyDeleted() returns all - niklas - 03-30-2020

Oh my god.. I think i should not develop in the morning before 2 cups of coffee to activate my brain.

The deleted_at row was not nullable so 0000-00-00 00:00 is default value of mySql. I change it to nullable and set defaultvalue to null and oh... it work's.

Thank you very much!!


RE: ->onlyDeleted() returns all - includebeer - 03-30-2020

(03-30-2020, 05:58 AM)niklas Wrote: Oh my god.. I think i should not develop in the morning before 2 cups of coffee to activate my brain.

Haha! Yeah, the golden rule is at least 1 cup of coffee in the morning, and no more than 2 beer in the evening!  Big Grin