通过一个查询删除cakephp3中所有关联表的数据

nwsw7zdq  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(307)

我有一个表products和一些表按product\u id与其连接。当从product表中删除产品时,我想从所有其他表中删除此product\u id的所有行。现在怎样才能方便地做呢?我尝试如下:

  1. $productTable = [
  2. 'related_products',
  3. 'quantities',
  4. 'products_tags',
  5. 'products_settings',
  6. 'products_details',
  7. 'categories_products',
  8. ];
  9. $tableObj = TableRegistry::get('Products');
  10. $query = $tableObj->query();
  11. $result = $query->deleteAll()
  12. ->contain($productTable)
  13. ->where(['store_id' => $storeId])
  14. ->execute();
hs1ihplo

hs1ihplo1#

在cakephp中,有一个从其他表中删除相关数据的依赖属性。
当依赖项设置为true时,实体被删除,关联的模型记录也被删除。在本例中,我们将其设置为true,这样删除用户也将删除其关联的地址。
要使用它,只需创建 association 并添加 dependent 是的。例如

  1. /* In a Table's initialize method. */
  2. $this->hasMany('Comments', [
  3. 'dependent' => true, /* Add this line */
  4. ]);

蛋糕->关联-将表链接在一起

相关问题