Support Joomla!

references:joomla.framework:database:jdatabase-insertobject

Table of Contents

insertObject

API Package Subpackage Class Method Reference Last reviewed Doc status
API Home Package Joomla.Framework Subpackage Database Class JDatabase Method insertObject Reference insertObject() Never Work in Progress

Inserts a new database table row using data from an object. Returns true if the insertion was successful; false otherwise.

Syntax

void insertObject ( $table, &$object, $keyName, $verbose )

$table string is a string containing the name of the database table.
&$object object is an object containing the database fields to be inserted. Attributes that are arrays or objects, have a null value or have a name beginning with an underscore, are ignored when setting the database fields.
$keyName string is a string containing the name of the primary key of the database table. If this is not null then the $keyname attribute of $object will be updated with the unique id number returned by the database insertion. This parameter is optional and if omitted defaults to null.
$verbose boolean is a flag. If true then the SQL query and the unique id returned by the query are output. If false then no output is produced. This parameter is optional and if omitted defaults to false.

Examples

This example inserts a new row in the Joomla! users table. Notice that the ‘id’ field is set to null before calling insertObject and will be updated with the unique id number allocated by the database.

Example

$database =& JFactory::getDBO();
$user = new stdClass;
$user->id = NULL;
$user->name = 'Linus Torvalds';
$user->username = 'linus';
 
if (!$database->insertObject( '#__users', $user, 'id' )) {
  echo $database->stderr();
}

echo $user->id;

might produce:

Result

65

Discussion

Full name:
E-Mail:
 
references/joomla.framework/database/jdatabase-insertobject.txt (983 views) · Last modified: 2007/08/15 14:53