php 如何使用 CakePHP 3 连接多个表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30413740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to join multiple tables using CakePHP 3?
提问by Wisd0m
I am using CakePHP 3.x.
我正在使用 CakePHP 3.x。
What I want is to be able to call $this->Categories->find() and then join Topics on Topics.cat_id = Categories.id and then join Posts on Posts.topic_id = Topics.id.
我想要的是能够调用 $this->Categories->find() 然后加入 Topics on Topics.cat_id = Categories.id 然后加入 Posts on Posts.topic_id = Topics.id。
I don't get any errors but the only data I'm getting back is the Categories data, i have tried LEFT and INNER join with no success. Any help would be greatly appreciated.
我没有收到任何错误,但我返回的唯一数据是类别数据,我尝试了 LEFT 和 INNER 连接,但没有成功。任何帮助将不胜感激。
The table relationships are:
表关系为:
CategoriesTable
分类表
$this->hasMany('Topics');
TopicsTable
话题表
$this->belongsTo('Categories');
$this->hasMany('Posts');
PostsTable
帖子表
$this->belongsTo('Topics');
Also the query i have:
还有我的查询:
$query = $this->Categories->find('all')
->order(['Categories.name' => 'ASC'])
->join([
'topics' => [
'table' => 'Topics',
'type' => 'LEFT',
'conditions' => 'topics.Cat_id = Categories.id'
],
'posts' => [
'table' => 'Posts',
'type' => 'LEFT',
'conditions' => 'posts.topic_id = topics.id'
]
]);
Tried using the containable behavior but now i'm getting the error "Categories is not associated with Posts" using this query:
尝试使用可包含的行为,但现在我使用以下查询收到错误“类别与帖子无关”:
$query = $this->Categories->find('all')
->order(['Categories.name' => 'ASC'])
->contain(['Topics', 'Posts' => function($q){
return $q->where(['Posts.topic_id' => 'Topics.id']);
}]);
回答by Wisd0m
With the advice @ndm gave i was able to get the result i wanted. I must have overlooked the section in the docs, so anyone else having this problem, here's how to do it.
根据@ndm 的建议,我能够得到我想要的结果。我一定忽略了文档中的部分,所以其他人有这个问题,这是怎么做的。
$query = $this->Categories->find('all')
->order(['Categories.name' => 'ASC'])
->contain([
'Topics.Posts.Users'
]);
回答by sambo
Just find the kind of legacy of cakephp2, you can still use join like the old version.
只要找到cakephp2的那种遗留,你仍然可以像旧版本一样使用join。
$result = $this->Category->findAll(fields=>['id','Topic.id'], 'conditions'=>['Topic.name'=>'s'],'join'=>['Topic' => [
'table' => 'topics',
'type' => 'INNER',
'conditions' => 'Topic.category_id = Category.id'
]]);
Find familiar? you still be able to use alias as before
找熟人?你仍然可以像以前一样使用别名
First time answer, not sure how the code editor works, sorry.
第一次回答,不确定代码编辑器是如何工作的,抱歉。
回答by Bàn Chan Tr?n
Try this:
尝试这个:
$query = $this->Categories->find('all')
->fields('Categories.*', 'Topics.*', 'Posts.*')
->order(['Categories.name' => 'ASC'])
->join([
'topics' => [
'table' => 'Topics',
'type' => 'LEFT',
'conditions' => 'topics.Cat_id = Categories.id'
],
'posts' => [
'table' => 'Posts',
'type' => 'LEFT',
'conditions' => 'posts.topic_id = topics.id'
]
]);
Add this line:
添加这一行:
->fields('Categories.*', 'Topics.*', 'Posts.*')
to retrieve data from Topics and Posts
从主题和帖子中检索数据