Welcome Guest, Not a member yet? Register   Sign In
->onlyDeleted() returns all
#1

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

Can you confirm there are rows with the deleted_at field with a NULL value? Maybe your previous delete() really deleted all the rows.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

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

(This post was last modified: 03-27-2020, 02:27 PM by includebeer.)

(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”?
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#5

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!!
Reply
#6

(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
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB