views:

152

answers:

1

What causes a row (Zend_Db_Table_Row) to be set to "readOnly?" I'm having trouble deleting rows in a loop:

// this is set to some integers
$ids = array();

// get the results
$results = $table->fetchAll($select);

foreach ($results as $result)
{
    $value = $result->value;
    if (!in_array($value, $ids))
    {
        // throws a "row is read-only" error
        $result->delete();
    }   
}

I want to delete the row's that aren't in the $ids array, but it throws an error saying the row is read only. I haven't set that flag or done anything with the row...any idea why it's read-only? Any help would be appreciated.

EDIT:

Here's my select:

$table = $options->joinModel;
$select = $table->select();
$select->from($table->getTableName(), array("id", "value" => $options->joinForeignKey))
       ->where("`{$options->foreignKey}` = ?", $row->id)
       ->group($options->joinForeignKey);
+3  A: 

A row is readOnly if the $select is such that prevents you from directly mapping fields back to a single origin row.

For example, if the $select involves a JOIN or a GROUP BY, it's not clear which row(s) would be affected if you change a value of a field in the row object.

You might say "I know which row is the source, why can't Zend_Db_Table_Row tell?" But there are many corner cases, so it's a hard problem to solve in general.

Keep in mind all of Zend_Db is under 3000 lines of code. It can't have a lot of magic in it.

A row object can also be readOnly if you've serialized and then deserialized it.

Bill Karwin
Oh, ok, so basically... selecting a row "AS value" ...the row's wouldn't be sure that value was a real field or not. Makes sense. I can get away without the alias, so it's an easy fix. Great answer!
Typeoneerror
Yes, and also because you `GROUP BY joinForeignKey`, then in theory there could be multiple rows in the group. So if you were to `$row->id = 1234`, which row in the table should it update?
Bill Karwin
Good point. I'm going to take that group out. Should be working now :) Thanks a mil, Bill.
Typeoneerror