finally i got something that’s challenging and twisting enough for you to conquer:
say i have these 3 tables:
tbl_follow, (records the follow relationship between users. if user A follows user B, a new record will be inserted:
following_id: the id of the following user
followed_id: the id of the followed user
tbl_move, (records the moves of every user, it has this attributes:
move_id: the id of the user who made this move.
The relationship between these 3 tables are clear: A user can have many followers and can be followed by many users. A user can have many moves.
Now the question is: How can I get a user’s followed users’ moves (AR) using CActiveDataProvider so that i can listed them somewhere?
I tried many times and find the relation too twisting to use our powerful CActiveDataProvider. But, perhaps some powerful mind can do?
$moveDataProvider = new CActiveDataProvider('move',array( //move, the table name
I don’t know why this would be challenging but:
Your User has relations:
// invalid Yii code
followers: array(self::MANY_MANY , follow, following_id, followed_id),
following: array(self::MANY_MANY , follow, following_id, followed_id),
moves : array(self::HAS_MANY, move, user_id),
so: all relations return arrays of objects
so if you want user x’s y followed usre’s moves where x i
$moveArray = $user->Following[x]->moves;
Thanks Plato, my tbl_user table does have these relations:
'myFollowings' => array(self::HAS_MANY, 'Follow', 'follower_uid'),
'myFollowers' => array(self::HAS_MANY, 'Follow', 'followed_uid'),
but i don’t see your code description can really work.
suppose i want to find $user’s followed users’ moves
$user->myFollowings is an array of tbl_follow objects, it can not be indexed with $user or $user->id. It can only be traversed with foreach.
what i want is to provider these moves in a CActiveDataProvider object
as I said
$moveArray = $user->following[x]->moves;
//where x is the position of the followed user in the following array