-
Bug
-
Resolution: Deferred
-
Minor
-
None
-
4.1.4
-
None
-
MOODLE_401_STABLE
When calling update_record() on a MySQL database object, if you misspell the table name, then an exception is thrown with the error message:
moodle_database::update_record_raw() id field must be specified.
This is misleading, as the id has been specified, and can cause the developer to spend time trying to work out why the id is not specified when the code appears to be setting it!
The problem is caused by update_record() getting a list of columns in the database table to be updated (which there aren't any if the table name is misspelt) and stripping out of the dataobject, any columns which are not in the database table (so it strips all fields out). It then passes the now empty object to update_record_raw() which finds the object no longer has an id column, so throws the above exception.
update_record() should spot that the list of columns for the table is empty, and then throw an exception that the user is trying to update a table which doesn't exist.
Current code:
$columns = $this->get_columns($table);
$cleaned = array();
Proposed code:
$columns = $this->get_columns($table);
if (empty($columns))
$cleaned = array();
Secondly, potentially, the code should throw an exception if a column is provided to the update function which doesn't exist, rather than just stripping it out silently.
Current code:
if (!isset($columns[$field]))
Proposed code:
if (!isset($columns[$field]))