1

I'm trying to achieve a cascading UPDATE and DELETE effect in a MyISAM database (similar effect as you can create in InnoDB tables with foreign keys). Two example tables:

  • albums
  • photos (has an album_id column that is a "foreign key" to the albums table)

Now when I delete a row in the albums table I would like Zend_Db_Table to automatically delete all related rows in the photos table. This is what I have in the albums table:

protected $_name = 'albums';

protected $_dependentTables = array(
    'Photos'
);

And I have this in the photos table:

protected $_name = 'photos';

protected $_referenceMap = array(
    'Album' => array(
        'columns' => array('album_id'),
        'refTableClass' => 'Albums',
        'refColumns' => array('id')
    )
);

Yes when I delete a row in the albums table, the photos from that album do not get removed.

This is how I'm removing the album:

public function remove($id)
{
    $where = $this->getAdapter()->quoteInto('id = ?', $id, 'INTEGER');
    return $this->delete($where);
}
4

1 回答 1

3

您需要设置级联删除。所以你的参考地图应该是:

protected $_referenceMap = array(
'Album' => array(
    'columns' => array('album_id'),
    'refTableClass' => 'Albums',
    'refColumns' => array('id'),
    'onDelete' => self::CASCADE
));

在此处查看级联操作的完整描述:http: //framework.zend.com/manual/en/zend.db.table.relationships.html#zend.db.table.relationships.cascading

注意 只有在结果集的实际行(即 Zend_Db_Table_Row 类)上调用函数时才会触发级联操作。在本例中触发删除功能:

$album = $albums->find($id);
$album->delete();//This triggers the cascading delete
于 2009-12-27T21:10:40.857 回答