Yii:内部加入3桌

时间:2013-05-15 15:07:56

标签: mysql yii inner-join

希望在我的网站上获得我所有的朋友活动。

我有3张桌子:

  • follow_users(user_idfollow_user_id
  • 视频(IDTitleDateLikesViewsActiveusers_id
  • videos_has_userlikes(idisLikedusers_idvideos_id

现在我想创建一个查询来获取登录用户的所有好友活动。

这样的事情:

$followers = Select * from follow_users where user_id = $MyId;

foreach ($followers as $userFollow) {

Select * from videos innerjoin videos_has_userlikes on videos.ID = videos_has_userlikes.videos_id where videos_has_userlikes.users_id = $userFollow;
}

我尝试使用Yii中的函数,但我不确定如何使用3个表的内连接..

 public function getFriendsLikeActivity($userId){
        $criteria = new CDbCriteria;
        $criteria->select = '*';
        $criteria->alias = 'videos, videos_has_userlikes, follow_users';
        $criteria->join = 'INNER JOIN videos_has_userlikes ON videos.ID = videos_has_userlikes.videos_id INNER JOIN follow_users ON videos.users_id = follow_users.user_id';
        $criteria->condition = 'follow_users.user_id =:usersID AND videos.users_id = :usersID AND Active=:active AND videos_has_userlikes.isLiked =:liked';
        $criteria->params = array(
            ':usersID' => $userId,
            ':active' => TRUE,
            ':liked' => TRUE,
        );
        $criteria->order = 'videos_has_userlikes.date Desc';        

        return Videos::model()->with('users')->findAll($criteria);
    }

请一些帮助:)

谢谢!

谢谢@deez!

但我只想要跟随用户的类似活动..

现在我从VideosHasUserLikes表中获取数据,但是没有与表followUser的关系。用户表和followUser表之间只有一个关系。有没有办法将这些表与$ criteria-> with()?

连接起来

现在我有了这段代码但是我收到了一个错误,因为它与'followUser'表没有关系..

public function getFriendsLikeActivity($userId){
        $criteria = new CDbCriteria;
        $criteria->select = '*';
        $criteria->alias = 'videos_has_userlikes';      
        $criteria->condition = 'videos.users_id = :usersID AND Active=:active AND videos_has_userlikes.isLiked =:liked';
        $criteria->params = array(
            ':usersID' => $userId,
            ':active' => TRUE,
            ':liked' => TRUE,
        );
        $criteria->with = array(
            'videos'=>array(
                'together'=>true,
                'select'=>'*',                
            ),
            'users'=>array(
                'together'=>true,
                'select'=>'*',
            ),  
            'followUser'=>array(
                'together'=>true,
                'select'=>'*',
            ), 
        );     

        return VideosHasUserLikes::model()->findAll($criteria);
    }

也许是这样的?

 public function getFriendsLikeActivity($userId){
            $criteria = new CDbCriteria;
            $criteria->select = '*';
            $criteria->alias = 'videos_has_userlikes';      
            $criteria->condition = 'videos.users_id = :usersID AND Active=:active AND videos_has_userlikes.isLiked =:liked';
            $criteria->params = array(
                ':usersID' => $userId,
                ':active' => TRUE,
                ':liked' => TRUE,
            );
            $criteria->with = array(
                'videos'=>array(
                    'together'=>true,
                    'select'=>'*',                
                ),
                'users'=>array(
                    'together'=>true,
                    'select'=>'*',
                    'with' => 'followUser'=>array(
                                  'together'=>true,
                                  'select'=>'*'),
            ), 

                ),                  
            );     

            return VideosHasUserLikes::model()->findAll($criteria);
        }

谢谢!

1 个答案:

答案 0 :(得分:1)

您应该考虑使用CDbCriteria-> with。

$criteria->with = array(
    'videos'=>array(
        'together'=>true,
        'select'=>'field1, field2, field3',
    ),
    'videos.likes'=>array(
        'together'=>true,
        'select'=>'field1, field2, field3',
    ),        
);

然后,您可以像$ model-> videos-> field1或$ model-> videos-> likes-> field1一样引用它。

http://www.yiiframework.com/doc/api/1.1/CDbCriteria#with-detail

编辑:

您应该展示您的关系以获得更多帮助,但我认为您想要的是设置从用户到user_likes的HAS_MANY关系,然后将其包含在with中。