with() CDbException

Hello all,

I am fairly new to yii and would appreciate any help with the following thing:

I am trying to execute an eager approach query but keep getting a CDbException that reads:

       The relation "role" in active record class "Member" is specified with an invalid foreign key. The format of the foreign key must be "joinTable(fk1,fk2,...)".

The tables involved here are :

member

±---------±----------±-----------±---------±---------------±-------±-------------±---------±-----------±---------+

| ID | FirstName | MiddleName | LastName | SecondLastName | Gender | UserName | Password | DateJoined | DateLeft |

±---------±----------±-----------±---------±---------------±-------±-------------±---------±-----------±---------+

role

±—±---------±---------±---------±--------------------+

| ID | MemberID | RecordID | RoleType | DateInserted |

±—±---------±---------±---------±--------------------+

These are my relation function inside their respective classes:

Member




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(

                    'role'  => array(self::MANY_MANY,'Role','MemberID'),

                    'record'=> array(self::MANY_MANY,'Record','MemberID'),

		);

	}



Role




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(

                    'member'      =>array(self::BELONGS_TO,'Member','MemberID'),

                    'record'      =>array(self::BELONGS_TO,'Record','RecordID'),

                    'relationship'=>array(self::HAS_MANY,'Relationship',array('RelationshipID'=>'id'),'through'=>'Relationship_Involves_Role'),

		);

	}



When executing this:




$userCredentials = Member::model()->with('role')->findAll();



I get the error mentioned above.

Any ideas why this is happening?

Hey all

Apparently I had the syntax incorrect for the realtions function, adding this helped:




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(

                    'role'  => array(self::MANY_MANY,'Role','Role(MemberID,ID)'),

                    'record'=> array(self::MANY_MANY,'Record','Record(MemberID,ID)'),

		);

	}



The only issue I now face is that the query does not return the role information, am I crazy to expect that data to be returned with the eager approach or am I doing something wrong?

thanks in advance

okay I figured it out already jaja

apparently I had to add a together=>true to the relation function


[code]

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(

                    'role'  => array(self::MANY_MANY,'Role','Role(MemberID,ID)','together'=>true),

                    'record'=> array(self::MANY_MANY,'Record','Record(MemberID,ID)','together'=>true),

                );

        }



[/code]

sorry for the bother

Thanks that really helped me out!

Here is a good page to see for the explanation: http://www.yiiframework.com/wiki/280/1-n-relations-sometimes-require-cdbcriteria-together/

\\Cheers//