It looks like you re using Zend_Db_Adapter to perform your queries so I m not sure you can do what I do, anyway here goes.
I usually use the Zend_Db_Table_Row save() method to insert and update records, however I also use the DbTable models to provide access to the Table and Table_Row abstract api s.
public function save(Music_Model_Artist $artist) {
//if id is not null
if (!is_null($artist->id)) {
//find row, uses fetchRow() so will return NULL if not found
$row = $this->findById($artist->id);
$row->id = $artist->id;
} else {
//if ID is null create new row
$row = $this->_getGateway()->createRow();
}
//assign data to row
$row->name = $artist->name;
//save new row or update
$row->save();
//return the row, in case I need it for something else
return $row;
}
这意味着,如果我在数据阵列(或在此情况下实体对象)中包含和标识该行,则该行将更新,如果对象中没有标识,则将创建新行。
如果你好奇我是如何 建造这堂课的:
public function __construct(Zend_Db_Table_Abstract $tableGateway = NULL) {
//pass in concrete DbTable Model, parent alts to using table name string to construct class
$this->_tableGateway = new Application_Model_DbTable_Artist();
parent::__construct($tableGateway);
}
希望这能帮上点忙