Zend_Db_Table 级联删除和更新

发布于 2024-08-16 03:41:56 字数 849 浏览 9 评论 0原文

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女皇必胜 2024-08-23 03:41:56

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

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

You need to setup the cascade delete. So your reference map should be:

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

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:

$album = $albums->find($id);
$album->delete();//This triggers the cascading delete
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文