Actions

JDatabase/updateObject

From Joomla! Documentation

Revision as of 07:48, 22 September 2009 by Nev3rm0re (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Updates an object in a database

Syntax

updateObject($tableName, &$object, $keyName, $updateNulls = true)

First parameter is a string containing database table name you are updating.

Second parameter is a PHP object, containing fields and their values. JDatabaseMySQL and JDatabaseMySQLi both use get_object_vars() function on this object, so only public properties will be saved to a DB.

Also, updateObject() will ignore all properties that are of type object or array, and also those whose name starts with underscore.

Third parameter is primary key name. Property with this name will not be included in update, instead it is going to be used for WHERE clause.

Fourth parameter tells JDatabase how to handle null values. If $updateNulls is false, updateObject() will not update fields that are null at all, otherwise it will set them to NULL.

 class user {
   public $id;
   public $username;
   public $password;
   public $last_login;
 }
 
 $user = new User();
 $user->id = 1;
 $user->username = 'username';
 $user->password = 'password';
 
 $db->updateObject('user', $user, 'id', false); // UPDATE `user` SET `username` = "username", `password` = "password" WHERE `id` = 1
 $db->updateObject('user', $user, 'id', true);  // UPDATE `user` SET `username` = "username", `password` = "password", last_login = NULL WHERE `id` = 1