想在我的网站上获得我所有的friendlikeactivity。
我有 3 张桌子:
- 关注用户 (
user_id
,follow_user_id
) - 视频 (
ID
,Title
,Date
,Likes
,Views
,Active
,users_id
) - video_has_userlikes (
id
,isLiked
,users_id
,videos_id
)
现在我想创建查询以获取已登录用户的所有 Friends Like Activity。
像这样的东西:
$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 没有关系。user 表和 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);
}
谢谢!