Welcome Guest, Not a member yet? Register   Sign In
Checking if delete operation was successful
#1

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

Donatas G.
Reply
#2

do not forget about SoftDelete
Reply
#3

(This post was last modified: 01-15-2023, 11:45 PM by dgvirtual.)

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

Donatas G.
Reply
#4

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
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(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/CodeIgni...ssues/4632 with SQLite3 specifics.
==

Donatas G.
Reply
#6

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

Donatas G.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB