0

Consider code below:

$select = $table->select()->setIntegrityCheck(false);
$row = $table->fetchRow($select);

// this throws Specified column "custom_data" is not in the row
$row->custom_data = 123;

echo $row->custom_data;

How can I add some custom data to zend db row?

1 Answer 1

2

If you want to add temporary data to the row object such that it doesn't actually get saved with the row, don't use attributes, use a setter method in your row class:

protected $customData = null;

public function getCustomData()
{
    return $this->customData;
}

public function setCustomData($data)
{
    $this->customData = $data;
    return $this;
}

Then just call that from your code:

$row->setCustomData($data);

Alternately, if you want to do this for many classes, you can override the __set() method of Zend_Db_Table_Row_Abstract such that instead of throwing an exception, it stores the value in a separate area:

protected $extraFields = [];

function __set($columnName, $value)
{
    $columnName = $this->_transformColumn($columnName);
    if (array_key_exists($columnName, $this->_data)) {
        $this->_data[$columnName] = $value;
        $this->_modifiedFields[$columnName] = true;
    } else {
        $this->extraFields[$columnName] = $value;
    }
}

public function __get($columnName)
{
    $columnName = $this->_transformColumn($columnName);
    if (array_key_exists($columnName, $this->_data)) {
        return $this->_data[$columnName];
    } else {
        return $this->extraFields[$columnName];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.