我最终扩展了Zend_Db_Table_Abstract
课程来实现这一点。_cascadeDelete
公共函数调用数据库适配器delete
函数。我进行了更改,以便改为delete
调用来自的函数。Zend_Db_Table_Row_Abstract
这使得记录删除递归。
更新:我添加了删除时设置为的代码self::SET_NULL
。
这是我的修改版本_cascadeDelete
:
/**
* Called by parent table's class during delete() method.
*
* @param string $parentTableClassname
* @param array $primaryKey
* @return int Number of affected rows
*/
public function _cascadeDelete($parentTableClassname, array $primaryKey)
{
$this->_setupMetadata();
$rowsAffected = 0;
foreach ($this->_getReferenceMapNormalized() as $map) {
if ($map[self::REF_TABLE_CLASS] == $parentTableClassname && isset($map[self::ON_DELETE])) {
switch ($map[self::ON_DELETE]) {
case self::CASCADE:
$select = $this->select();
for ($i = 0; $i < count($map[self::COLUMNS]); ++$i) {
$col = $this->_db->foldCase($map[self::COLUMNS][$i]);
$refCol = $this->_db->foldCase($map[self::REF_COLUMNS][$i]);
$type = $this->_metadata[$col]['DATA_TYPE'];
$select->where($this->_db->quoteIdentifier($col, true) . ' = ?',
$primaryKey[$refCol], $type);
}
$rows = $this->fetchAll($select);
$rowsAffected += count($rows);
foreach ($rows as $row) {
$row->delete();
}
break;
case self::SET_NULL: {
$update = array();
$where = array();
for ($i = 0; $i < count($map[self::COLUMNS]); ++$i) {
$col = $this->_db->foldCase($map[self::COLUMNS][$i]);
$refCol = $this->_db->foldCase($map[self::REF_COLUMNS][$i]);
$type = $this->_metadata[$col]['DATA_TYPE'];
$update[$col] = null;
$where[] = $this->_db->quoteInto(
$this->_db->quoteIdentifier($col, true) . ' = ?',
$primaryKey[$refCol], $type);
}
$rowsAffected += $this->update($update, $where);
break;
}
default:
// no action
break;
}
}
}
return $rowsAffected;
}