PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Writing rows to the database

Changing column values in a row

You can set individual column values using column accessors, similar to how the columns are read as object properties in the example above.

Using a column accessor to set a value changes the column value of the row object in your application, but it does not commit the change to the database yet. You can do that with the save() method.

Example 306. Example of changing a column in a row

<?php
$bugs 
= new Bugs();
$row $bugs->fetchRow($bugs->select()->where('bug_id = ?'1));

// Change the value of one or more columns
$row->bug_status 'FIXED';

// UPDATE the row in the database with new values
$row->save();

Inserting a new row

You can create a new row for a given table with the createRow() method of the table class. You can access fields of this row with the object-oriented interface, but the row is not stored in the database until you call the save() method.

Example 307. Example of creating a new row for a table

<?php
$bugs 
= new Bugs();
$newRow $bugs->createRow();

// Set column values as appropriate for your application
$newRow->bug_description '...description...';
$newRow->bug_status 'NEW';

// INSERT the new row to the database
$newRow->save();

The optional argument to the createRow() method is an associative array, with which you can populate fields of the new row.

Example 308. Example of populating a new row for a table

<?php
$data 
= array(
    
'bug_description' => '...description...',
    
'bug_status'      => 'NEW'
);

$bugs = new Bugs();
$newRow $bugs->createRow($data);

// INSERT the new row to the database
$newRow->save();

Note

The createRow() method was called fetchNew() in earlier releases of Zend_Db_Table. You are encouraged to use the new method name, even though the old name continues to work for the sake of backward compatibility.

Changing values in multiple columns

Zend_Db_Table_Row_Abstract provides the setFromArray() method to enable you to set several columns in a single row at once, specified in an associative array that maps the column names to values. You may find this method convenient for setting values both for new rows and for rows you need to update.

Example 309. Example of using setFromArray() to set values in a new Row

<?php
$bugs 
= new Bugs();
$newRow $bugs->createRow();

// Data are arranged in an associative array
$data = array(
    
'bug_description' => '...description...',
    
'bug_status'      => 'NEW'
);

// Set all the column values at once
$newRow->setFromArray($data);

// INSERT the new row to the database
$newRow->save();

Deleting a row

You can call the delete() method on a Row object. This deletes rows in the database matching the primary key in the Row object.

Example 310. Example of deleting a row

<?php
$bugs 
= new Bugs();
$row $bugs->fetchRow('bug_id = 1');

// DELETE this row
$row->delete();

You do not have to call save() to apply the delete; it is executed against the database immediately.

Zend Framework