?
avatar olleharstedt
olleharstedt
6 Nov 2018

Steps to reproduce the issue

$result = $table->store();

Expected result

If database insert or update failed, $result should be false.

Actual result

$result is true by default, and only changed via the onAfterStore event.

System information (as much as possible)

Latest Joomla.
PHP 5.5.9.

Additional comments

Probably by design, but I think this is worth a discussion. When checking $result, what I'm interested in is if the database got updated. The only way to do this now is to manually read the database after store(), which is awkward.

avatar olleharstedt olleharstedt - open - 6 Nov 2018
avatar joomla-cms-bot joomla-cms-bot - change - 6 Nov 2018
Labels Added: ?
avatar joomla-cms-bot joomla-cms-bot - labeled - 6 Nov 2018
avatar olleharstedt olleharstedt - change - 6 Nov 2018
The description was changed
avatar olleharstedt olleharstedt - edited - 6 Nov 2018
avatar mbabker
mbabker - comment - 6 Nov 2018

The database's execute() method really doesn't return a boolean true/false anymore to track this kind of thing. These days, you basically should assume any database transaction succeeds unless an Exception is thrown.

avatar olleharstedt
olleharstedt - comment - 6 Nov 2018

We believe (not 100% sure) we had a silent fail this week, leading to a customer not getting his product. Would you recommend any special function to use, like checking last inserted id or similar?

avatar olleharstedt olleharstedt - change - 6 Nov 2018
The description was changed
avatar olleharstedt olleharstedt - edited - 6 Nov 2018
avatar mbabker
mbabker - comment - 6 Nov 2018

$db->insertid() for the last inserted ID from the last INSERT statement.

Otherwise, if you had "a silent fail" you'd need to determine if that was because of a server level error (which should be caught in the database error handling system) or if it was because a query ended up updating 0 rows (which wouldn't be an error handled in our code; the query still executed with no error response). If it was indeed a server level error that was not caught, that's a bug and a reproducer would need to be given so we can try and recreate the issue and fix it.

avatar olleharstedt
olleharstedt - comment - 6 Nov 2018

Thank you for your comment.

Maybe we should reconsider this doc line, since it's not technically correct? https://github.com/joomla/joomla-cms/blob/staging/libraries/src/Table/Table.php#L772

To be correct, the method would have to check the result from insertObject and updateObject.

avatar mbabker
mbabker - comment - 6 Nov 2018

There are still bits of the table's store() method that can return boolean false, so it is still accurate. What it does not do is catch database exceptions and convert to the getError/setError handling mechanism and return boolean false if an exception is thrown triggering insertObject/updateObject.

Checking for a boolean return from insertObject or updateObject is unreliable. In the case of insertObject, the false return can only be triggered if a driver's execute() method returns something falsy (non-strict, meaning this would be caught if the driver returned a null value, boolean false, numeric 0, or string '0', among other potential weirdness). None of the drivers have a falsy return, they are all returning something (for the PDO drivers the executed statement; for non-PDO drivers it looks like that is the executed database resource), so for all intents and purposes insertObject will only ever return true. For updateObject, it direct returns the result of the driver's execute() method, so that will never have a falsy return.

Long and short, if an exception is not thrown, assume there was no error, unless it can be proven there is a flaw in our error detection code (in which case, as I said we need something to work with to fix it).

avatar olleharstedt
olleharstedt - comment - 6 Nov 2018

Yes, I was reading through the execute() method and realize it would always throw an exception at error. Is there a way to check which driver we use (mysql or mysqli)?

avatar mbabker
mbabker - comment - 6 Nov 2018

public $dbtype = 'foo'; in your configuration.php file.

avatar olleharstedt
olleharstedt - comment - 6 Nov 2018

Thanks!

avatar olleharstedt olleharstedt - change - 6 Nov 2018
Status New Closed
Closed_Date 0000-00-00 00:00:00 2018-11-06 16:18:37
Closed_By olleharstedt
avatar olleharstedt olleharstedt - close - 6 Nov 2018

Add a Comment

Login with GitHub to post a comment