ZF-3114: Zend_Db_Table_Row_Abstract-->__set() modified detection

Description

I'm just gonna give an example. Please delete/close this if I'm wrong.

my current code:


if($photo->albumId    != $albumId)    $photo->albumId = $albumId;
if($photo->name        != $name)    $photo->name = $name;
$photo->save();

better code:


$photo->albumId = $albumId;
$photo->name = $name;
$photo->save();

fix required: in Zend_Db_Table_Row_Abstract old:


public function __set($columnName, $value)
{
    $columnName = $this->_transformColumn($columnName);
    if (!array_key_exists($columnName, $this->_data)) {
        require_once 'Zend/Db/Table/Row/Exception.php';
        throw new Zend_Db_Table_Row_Exception("Specified column \"$columnName\" is not in the row");
    }
    $this->_data[$columnName] = $value;
    $this->_modifiedFields[$columnName] = true;
}

new:


public function __set($columnName, $value)
{
    $columnName = $this->_transformColumn($columnName);
    if (!array_key_exists($columnName, $this->_data)) {
        require_once 'Zend/Db/Table/Row/Exception.php';
        throw new Zend_Db_Table_Row_Exception("Specified column \"$columnName\" is not in the row");
    }
    
    if($this->_data[$columnName] != $value) {
        $this->_data[$columnName] = $value;
        $this->_modifiedFields[$columnName] = true;
    }
}

This is not tested. Just to make clear what I want.

version: trunk 9228

// edit: made code readable // edit2: second attempt :D

Comments

Please evaluate and categorize as necessary.

This is not an issue; if you examine the code of {{_doUpdate()}}, you'll see that this functionality already exists. For example:

        /**
         * Compare the data to the modified fields array to discover
         * which columns have been changed.
         */
        $diffData = array_intersect_key($this->_data, $this->_modifiedFields);

Please feel free to reopen if I have missed something. :)

array_intersect_key() does not evaluate the value at all. I just checks the keys. http://de.php.net/manual/en/…

and ``` is just "true" anyway

so I think whats happening is that doUpdate updates all fields, even if value was "changed" but remains the same.

maybe you didn't understood my point. I was saying that when sql returns me a "name=fritz" and I do tableRow->name = "fritz" than I did not change the value - no need to include it into doUpdate()

I see now; thanks for clarifying this. :)

It's a little more complicated than a simple '!='. For example, we must differently manage 'null', '', 'false' and '0'.

!== ;-)

See http://mikaelkael.fr/ZF-3114.php for a first try. We must read by lines (not by columns).

Bulk change of all issues last updated before 1st January 2010 as "Won't Fix".

Feel free to re-open and provide a patch if you want to fix this issue.