Problem with MANY_MANY relation

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?

I think you should start with reading tutorial… http://www.yiiframework.com/doc/guide/1.1/en/database.arr

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:


'addedFriends' => array(self::MANY_MANY, 'User', 'user_user(added_user_id, received_user_id)'),

BELONGS_TO should only specify column name in current model that reference target model primary key:


'addedUser' => array(self::BELONGS_TO, 'User', 'added_user_id'),

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.

But my table has two attributes. How can I check them?

my user_user table have a accepted attribute and a date_create attribute. How can I verify this two attributes without using a ActiveRecord model?

I made it work. But how can I save things in this table and how can i consult this table attribute ?




            '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?

How OP? How did you make it work?