【问题标题】:How can I set associations between models in Cake PHP 3.5?如何在 Cake PHP 3.5 中设置模型之间的关联?
【发布时间】:2018-01-18 15:27:27
【问题描述】:

我在 MySql 中有一个这样的数据库结构。我使用 Cake PHP 3.5。

我想在 table1 中添加一个到 table4 的 belongsToMany 连接。并将其逆也添加到 table4。

    $this->belongsToMany('Table4', /*What goes here*/);

table4 是什么?

我可以使用 belongsToMany 中的查找器进行此加入吗?

谢谢!

【问题讨论】:

  • CakePHPs 内置的多对多关联不支持跨越多个连接表。我建议您详细说明您尝试使用该架构解决的实际问题,然后也许有人可以建议一种使用现有 CakePHP 功能实现解决方案的方法。

标签: php mysql sql cakephp orm


【解决方案1】:

正如 ndm 在 cmets Cake 中所说的那样,Cake 不支持您想要做的事情,只是出于某种原因,这不是多对多关系,请通过迁移创建数据库,首先https://book.cakephp.org/3.0/pt/migrations.html 然后(烘烤迁移,执行迁移)您的数据库将存在,您可以运行烘焙以创建模型,然后您将意识到您可以构建一个类似的查询

$this->table1->table2->table3->table4->find()->where([conditions]);

或其他类似

$this->table1->find()->contain(['table2' => ['table3' => ['table4' => 'conditions' => ['RealConditions']]]])->first();

如果您不想学习所有这些,可以通过paintfull 路径。或者只是先阅读一下https://book.cakephp.org/3.0/en/orm/associations.html#belongstomany-associations

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多