Issues

ZF-174: Documentation mistake in 'Adding Domain Logic' example

Description

In section 3.3.10. Adding Domain Logic an example is given of over-riding the Zend_Db_Table::update() method to automatically update a timestamp field:


    public function update($data)
    {
        // add a timestamp
        if (empty($data['updated_on'])) {
            $data['updated_on'] = time();
        }
        return parent::update($data);
    }

This is faulty because the parent method expects to be passed the WHERE clause for the row being updated:


(extract from Zend_Db_Table)

    /**
     * Updates existing rows.
     *
     * Columns must be in underscore format.
     *
     * @param array $data Column-value pairs.
     * @param string $where An SQL WHERE clause.
     * @return int The number of rows updated.
     */
    public function update(&$data, $where)
    {
        return $this->_db->update(
            $this->_name,
            $data,
            $where
        );
    }

Omitting the WHERE clause will cause a database error as an update to the primary key for all rows is attempted. The example should be changed to:


    public function update($data, $where)
    {
        // add a timestamp
        if (empty($data['updated_on'])) {
            $data['updated_on'] = time();
        }
        return parent::update($data, $where);
    }

Comments

Changing fix version to 0.8.0.

Recategorize as Documentation, Zend_Db.

The section on Zend_Db_Table was rewritten in revision 4200. The error mentioned above is now fixed.

Bookkeeping. Closing old issues and assigning them to the person who ultimately resolved the issue.