CodeIgniter Forums
Checking if delete operation was successful - 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: Checking if delete operation was successful (/showthread.php?tid=86143)



Checking if delete operation was successful - dgvirtual - 01-15-2023

In my controller I do
PHP Code:
$meetingsModel->delete(55);
$no $meetingsModel->db->affectedRows();
echo 
$no
or, alternatively:
PHP Code:
$db db_connect();
$meetingsModel->delete(55);
$no $db->affectedRows();
echo 
$no
The script always outputs 1, both when the row 55 existed and was deleted and when it did not exist (and so was not deleted, and no rows were actually affected).
I am using SQLite3 database.
1. Is this a bug?
2. What other method could I use to check if the deletion was successful? I can only think of making another query to check if row 55 is still in the database table...


RE: Checking if delete operation was successful - Mni.day - 01-15-2023

do not forget about SoftDelete


RE: Checking if delete operation was successful - dgvirtual - 01-15-2023

(01-15-2023, 12:33 PM)Mni.day Wrote: do not forget about SoftDelete
I am not using it, but still, soft delete would insert delete date, so affectedRows would normally report the number of soft-deleted entries as well.


RE: Checking if delete operation was successful - InsiteFX - 01-16-2023

Code:
$sqlite = new SQLite3('database.sqlite');
$statement = $sqlite->prepare('DELETE FROM mytable WHERE myclause');
$statement->execute();
echo $sqlite->changes();

Count The Number Of Rows Modified


RE: Checking if delete operation was successful - dgvirtual - 01-16-2023

(01-16-2023, 12:40 AM)InsiteFX Wrote:
Code:
$sqlite = new SQLite3('database.sqlite');
$statement = $sqlite->prepare('DELETE FROM mytable WHERE myclause');
$statement->execute();
echo $sqlite->changes();

Count The Number Of Rows Modified
Hi, thanks, looks like it should work, although in my case it will bypass the model hooks so I will go with repeated find for now... I did not notice any disclaimer regarding different behaviour of $db->rowsAffected() when using SQLite3 db, so I should probably report is as a bug.

I will see if I should open something like https://github.com/codeigniter4/CodeIgniter4/issues/4632 with SQLite3 specifics.


RE: Checking if delete operation was successful - dgvirtual - 11-24-2023

Today I checked my original code in the first post, and now, on Codeigniter 4.4.3, it works as expected, deleted row number is reported correctly (0 if nothing is deleted, 1 if one row...). Works with both mysql and sqlite3. So, problem solved.