【问题标题】:Creating left joins in cakephp在 cakephp 中创建左连接
【发布时间】:2013-12-13 16:53:17
【问题描述】:

我正在尝试在 cakephp 中使用左连接选择数据,这里我如何使用两个以上的表进行左连接。这里我在两个表 news_feeds 和 news_feed_likes 之间创建左连接。我想在这里再介绍两个表,NewsFeedComments和 newsfeed_likes_cmets。我该怎么做?

感谢您的帮助

$this->paginate = array(
    'conditions' => array('NewsFeed.group_id'=>$groupdata['Group']['id'],'NewsFeed.status'=>'A'),
    'joins' => array(
        array(
            'alias' => 'newslikes',
            'table' => 'news_feed_likes',
            'type' => 'LEFT',
            'conditions' => array(
                'newslikes.news_feed_id = NewsFeed.id',
                'newslikes.user_id'=>$this->Auth->user('id'),
                'newslikes.status'=>'1',
            ),
        ),
        array(
            'alias' => 'newscommentslikes',
            'table' => 'news_feed_comments_likes',
            'type' => 'LEFT',
            'conditions' => array(
                'newscommentslikes.news_feed_comment_id = NewsFeedComment.id',
                'newscommentslikes.user_id'=>$this->Auth->user('id'),
                'newscommentslikes.status'=>'1',
            ),
        ),
    ),
    'fields' => array(
        'IFNULL(newslikes.user_id,0) AS likestatus', 
        'NewsFeed.id', 
        'NewsFeed.posted_message', 
        'NewsFeed.created', 
        'NewsFeed.user_id', 
        'NewsFeed.status', 
        'NewsFeed.newslike', 
        'IFNULL(newslikes.status,0) AS status',
     ),
    'order' => array('NewsFeed.created' => 'desc'),
);

$this->set('newsfeed', $this->paginate( $this->NewsFeed ) );

【问题讨论】:

  • 'joins' => array(array(..first join..), array(..second join..), ..., array(.. nth join..)); 您是否尝试过向 joins 数组添加更多数组?
  • 将尝试此操作并回复您
  • 我已经检查过了,请检查我上面的代码,它给了我错误 Unknown column 'NewsFeedComment.id' in 'on Clause',在这里我如何使用另一个模型 NewsFeedComment。
  • 如果 news_feed_cmets 没有 newscmetslikes,基本上应该返回 0

标签: cakephp


【解决方案1】:

我的第一个建议是考虑将其移至模型中。但是,要回答具体问题,只需根据外键添加更多联接。因此,例如,如果您在 NewsFeedLike 和 NewsFeedLikeComments 之间有一个外键,您可以像这样添加另一个联接:

'joins' => array(
    array(
        'table' => 'news_feed_like_comments',
        'alias' => 'NewsFeedLikeComment',
        'type' => 'LEFT',
        'conditions' => array(
            'NewsFeedLikeComment.news_feed_like_id = NewsFeedLike.id',
        ),
    ),

因为您已经加入 NewsFeedLike,它将使用这些条件来限制 NewsFeedLikeComment。

另一个提示:编写代码时始终遵循 CakePHP 编码标准。它会让你的生活少很多痛苦。避免为您的模型使用带下划线的小写别名。总是用大写的第一个字母和单数写他们的驼峰式。所以在你的连接中,你应该使用NewsLikeNewsCommentLike,而不是newslikesnewscommentslikes 作为别名。

【讨论】:

    【解决方案2】:

    您可以使用以下语法连接多个表:

      $this->paginate = array(
            'conditions' => array(),
            'joins' => array(
                array(
                    'alias' => 'TableAlias1',  
                    'table' => 'table_name_1',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'TableName.id = table_name_1.id',
                    ),
                ),
                array(
                    'alias' => 'TableAlias2',  
                    'table' => 'table_name_2',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'TableName.id = table_name_2.id',
                    ),
                ),
                array(
                    'alias' => 'TableAlias3',  
                    'table' => 'table_name_3',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'TableName.id = table_name_4.id',
                    ),
                )
            ),
            'fields' => array(),
            'order' => array()
        );
    

    【讨论】:

    • 虽然您的联接可能有效,但编码标准建议您应匹配条件中的别名。例如,您将第一次加入条件设置为'TableName.id = table_name_1.id'。但是您的别名是TableAlias1。您的条件应与这样的别名匹配:'TableName.id = TableAlias1.id'.
    • 是的 Chuck...别名应该匹配
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-16
    相关资源
    最近更新 更多