PostNuke: A Flexible Open Source Content Management System
home | forum | international support | contact us

Documentation Wiki

Insert Operations


This section will cover two insert methods. The first to insert one row into the database, and the second will insert multiple rows.

insertObject


All tables in a database should have an ID field which is unique. DBUtil always assumes the default idcolumn is called 'id' unless otherwise told. By default, DBUtil::insertObject will also allocate a unique ID for the record.

DBUtil::insertObject($obj, $tablename, $force, $idcolumn)

    // simple insert
    // create an array object containing the field names on the left as defined in **pntabled.php**, and the values on the right
    $obj = array('name'    => 'fred',
                 'address' => 'bloggs',
                 'phone'   => '555-123-4321');

    // do the insert
    $result = DBUtil::insertObject($obj, 'customers');


$result will contain the ADODB recordset object or false on fail.

Assuming the insert is successful $obj will have been automatically been updated with the id of the insert and will now read:

    array('id'      => '1',
          'name'    => 'fred',
          'address' => 'bloggs',
          'phone'   => '555-123-4321');


If we need to chose the value for the ID field, we can do so by using the $force = true e.g.

    // forced insert
    // create an array object containing the field names on the left, and the values on the right
    $obj = array('id'      => '108',
                 'name'    => 'fred',
                 'address' => 'bloggs',
                 'phone'   => '555-123-4321');

    // force the insert
    $result = DBUtil::insertObject($obj, 'customers', true);


Needless to say if the ID field wasnt unique, the insert would fail. If our ID column is named something else, like 'cid' we would just add that to the command.

    $result = DBUtil::insertObject($obj, 'customers', false, 'cid');


insertObjectArray


DBUtil::insertObjectArray($obj, $tablename, $force, $idcolumn)

This function will perform multiple inserts. The object is an array of arrays like this example:

    // simple insert
    // create an array object containing the field names on the left as defined in **pntabled.php**, and the values on the right
    $obj = array(array('name'    => 'fred',
                       'address' => 'bloggs',
                       'phone'   => '555-123-4321'),
                 array('name'    => 'sally',
                       'address' => 'jane',
                       'phone'   => '555-321-1234'));
 
    // do the insert
    $result = DBUtil::insertObjectArray($obj, 'customers');



Previous | Top | Select Operations

CategoryDeveloperDocs
XML Revisions of $tag
Page history :: Last Editor [ Chestnut ] :: Owner [ markwest ] ::
Valid XHTML :: Valid CSS :: Powered by pnWikka 1.0 (A wiki fork from WikkaWiki)
 

Main Menu

Extensions Database

Documentation

Development

Login





 


 Log in Problems?
 New User? Sign Up!

Donate to PostNuke