有沒有可能的組合如何構建parent::update()
方法的參數以獲得最終更新查詢。原因是因爲Db表update
方法只是將您的$data
和$where
變量傳遞給Db適配器的方法update
。適配器的update
方法沒有留下附加信息的餘地。 你不能破解參數
如果你不能使用表關係與級聯更新。最好的辦法是擴展Db適配器並創建一個新的方法來處理這些類型的更新。這應該工作。
/**
* Add this method to you custom adapter
* Direct copy of update method with integration of $from
* @see Zend_Db_Adapter_Abstract::update
**/
public function updateFrom($table, $from, array $bind, $where = '')
{
/**
* Build "col = ?" pairs for the statement,
* except for Zend_Db_Expr which is treated literally.
*/
$set = array();
$i = 0;
foreach ($bind as $col => $val) {
if ($val instanceof Zend_Db_Expr) {
$val = $val->__toString();
unset($bind[$col]);
} else {
if ($this->supportsParameters('positional')) {
$val = '?';
} else {
if ($this->supportsParameters('named')) {
unset($bind[$col]);
$bind[':col'.$i] = $val;
$val = ':col'.$i;
$i++;
} else {
/** @see Zend_Db_Adapter_Exception */
require_once 'Zend/Db/Adapter/Exception.php';
throw new Zend_Db_Adapter_Exception(get_class($this) ." doesn't support positional or named binding");
}
}
}
// Reason #1 you can't hack into $data array to pass reference to a table
$set[] = $this->quoteIdentifier($col, true) . ' = ' . $val;
}
$where = $this->_whereExpr($where);
/**
* Build the UPDATE statement
*/
$sql = "UPDATE "
. $this->quoteIdentifier($table, true)
. ' SET ' . implode(', ', $set)
. ' FROM ' . $this->quoteIdentifier($from, true) // My only edit
. (($where) ? " WHERE $where" : ''); // Reason #2 no room in where clause
/**
* Execute the statement and return the number of affected rows
*/
if ($this->supportsParameters('positional')) {
$stmt = $this->query($sql, array_values($bind));
} else {
$stmt = $this->query($sql, $bind);
}
$result = $stmt->rowCount();
return $result;
}
/** Add this to your extended Zend_Db_Table **/
public function update(array $data, $where)
{
$tableSpec = ($this->_schema ? $this->_schema . '.' : '') . $this->_name;
$from = 'schema.name'; // Get from table name
return $this->_db->updateFrom($tableSpec, $from, $data, $where);
}
注:我沒有測試了這一點,但我非常有信心預期它會工作。如果您有任何問題,請告訴我。因爲我複製了適配器的更新方法,所以我繼續添加註釋,說明爲什麼不能破解這些參數。
編輯 我差點忘了提。每個適配器都是獨一無二的,因此您必須檢查適配器更新方法。我剛剛從Zend_Db_Abstract
複製。