$result = $table->store();
If database insert or update failed, $result should be false.
$result is true by default, and only changed via the onAfterStore event.
Latest Joomla.
PHP 5.5.9.
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.
Labels |
Added:
?
|
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?
$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.
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.
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).
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)?
public $dbtype = 'foo';
in your configuration.php
file.
Thanks!
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2018-11-06 16:18:37 |
Closed_By | ⇒ | olleharstedt |
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.