CakePHP 3使用多个连接ORM进行查询

时间:2016-02-04 18:10:46

标签: sql cakephp cakephp-3.0

我想做什么:

  • 加入多个表格。
  • 读取数据。
  • 如果可能,请创建具有多个维度的数据数组。

我有一个像这样的数据库: 表格文章,标签,用户,类别

加入:

  • 文章需要带标签的内部联接。 (多对多关系)
  • 使用用户标记内部联接。 (多对一的关系)
  • 使用类别标记内部联接。 (多对一的关系)

到目前为止我的查询:

private function findAllArticleRelatedStuff() {
$query = $articles->find();
$query->innerJoinWith('Tags', function ($q) {
    $q->innerJoinWith('Users');
    $q->innerJoinWith('Categories');
    return $q;
});
$query->select(['Articles.articlename', 'Tags.tagname', 'Users.username', 'Categories.categoriename']);
return $query;

}

我想选择articlesname,tagsname,usersname,categoriesname并首先打印它(在视图中,已经通过set()完成了它)。

foreach($query as $article) {
    debug($article->articlename); // works fine

    foreach($article as $tag) {
        debug($tag->tagname); // no output, no error

        // other foreach loops or something...
    }
}

我想我在这里犯了一个愚蠢的错误。

你可以给我一个关于创建这些值的数组的建议吗?我以为我在CakePHP中读到了一些数组方法。

编辑:(找到解决方案) 我没有正确地读出数据:

foreach($query as $article) {
    debug($article->articlenamename);
    debug($article->_matchingData["Tags"]["tagname"]);
}

1 个答案:

答案 0 :(得分:0)

尝试调试($ article-> tag-> tagname);

相关问题