Zend_Db_Table 级联删除和更新
我试图在 MyISAM 数据库中实现级联 UPDATE 和 DELETE 效果(与您可以在带有外键的 InnoDB 表中创建的效果类似)。两个示例表:
- albums
- photos (有一个 album_id 列,是 albums 表的“外键”)
现在,当我删除 albums 表中的一行时,我希望 Zend_Db_Table 自动删除 photos 表中的所有相关行。这是我在相册表中的内容:
protected $_name = 'albums';
protected $_dependentTables = array(
'Photos'
);
我在照片表中的内容是:
protected $_name = 'photos';
protected $_referenceMap = array(
'Album' => array(
'columns' => array('album_id'),
'refTableClass' => 'Albums',
'refColumns' => array('id')
)
);
是的,当我删除相册表中的一行时,该相册中的照片不会被删除。
这就是我删除相册的方法:
public function remove($id)
{
$where = $this->getAdapter()->quoteInto('id = ?', $id, 'INTEGER');
return $this->delete($where);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要设置级联删除。所以你的参考图应该是:
在这里查看级联操作的完整描述:http://framework.zend.com/manual/en/zend.db.table.relationships.html#zend.db.table.relationships.cascading
注意级联仅当对结果集的实际行(即 Zend_Db_Table_Row 类)调用函数时才会触发操作。本例中要触发删除函数:
You need to setup the cascade delete. So your reference map should be:
See full description of cascading operations here: http://framework.zend.com/manual/en/zend.db.table.relationships.html#zend.db.table.relationships.cascading
NB Cascading operations are only triggered when functions called on the actual row of the results set (i.e. Zend_Db_Table_Row class). To trigger the delete function in this example: