Hi, I have 2 models, User(users table) and Friendship(user_user table).
Here are the model relations
class User extends CActiveRecord {
.
.
.
public function relations() {
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
//Format 'variable' => array('relation_type', 'className', 'joinTable(attribute)),
return array(
'addedFriends' => array(self::MANY_MANY, 'Friendship', 'user_user(added_user_id)'),
'receivedFriends' => array(self::MANY_MANY, 'Friendship', 'user_user(received_user_id)'),
);
}
.
.
.
}
and my Friendship relations
class Friendship extends CActiveRecord {
public function relations() {
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'addedUser' => array(self::BELONGS_TO, 'Users', 'users(id)'),
'receivedUser' => array(self::BELONGS_TO, 'Users', 'users(id)'),
);
}
}
My User model is working fine, but when I try to do a var_dump($user->addedFriends); I receive a DATABASE ERROR(I’ll post it later, I’m at school now).
There’s something wrong with my defined relations in my Models?
MANY_MANY relationships have to specify pivot table with TWO column names - first relating to current model primary key, second relating target model primary key:
if you use MANY_MANY relationship you do not need ActiveRecord class for pivot table. It won’t be used anyway. If you need model for that table - you need to create HAS_MANY ralations in User model referencing Friendship model and BELONGS_TO relationships in Friendship model pointing to User model.
'addedFriends' => array(self::MANY_MANY, 'User', 'user_user(added_user_id, received_user_id)'), //Friends who User added
'receivedFriends' => array(self::MANY_MANY, 'User', 'user_user(received_user_id,added_user_id)'), //Friends who user received
I have this in my relations.
I get all addedFriends using $user->addedFriends.
But in my table I have a condition ‘accepted’. How do I select all addedFriends where accepted = true?