Php Zend DB行的自定义属性
考虑下面的代码:Php Zend DB行的自定义属性,php,database,zend-framework,Php,Database,Zend Framework,考虑下面的代码: $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; 如何向zend db row添加一些自定义数
$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;
如何向zend db row添加一些自定义数据?如果要向row对象添加临时数据,使其不会与该行一起保存,请不要使用属性,在row类中使用setter方法:
protected $customData = null;
public function getCustomData()
{
return $this->customData;
}
public function setCustomData($data)
{
$this->customData = $data;
return $this;
}
然后从代码中调用它:
$row->setCustomData($data);
或者,如果要对许多类执行此操作,可以重写Zend_Db_Table_Row_Abstract的uu set()方法,这样它就不会引发异常,而是将值存储在单独的区域中:
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];
}
}