Multiple relations to same table

Hi,

I’ve been unsuccessfully searching the forum for a way to declare the relations in a model when multiple fields are to be related to the same field in another table.

Table 1:

Name: User

Fields:

  User_Id


  User_Name


  ....

Table 2:

Name: PM

Fields:

  PM_Id


  PM_From


  PM_To


  PM_Text


  ....

The fields PM_From and PM_To are values who have a MANY_TO_ONE relation to User_Id.

But I have no idea how to declare those relations in my PM model.

Hopefully one of you can help me.

hi,

try make custom table for this. where PM is your PRIMARY KEY.

CREATE TABLE CustomTable ( PMID INT NOT NULL AUTO_INCREMENT ,

Name VARCHAR( 255 ) NOT NULL ,

Value TEXT NOT NULL ,

PRIMARY KEY ( PMID ) ) ENGINE = MYISAM ;

I don’t see where you want to go with this.

The entire database is normalized so normally there shouldn’t be a need for another table to connect both tables.

Maybe it’s helpful to explain what the PM table contains. They are internal messages from one user to another.

The PM_To and PM_From fields are the User_Id of the recipient and sender of the message.

try to read http://www.yiiframework.com/doc/guide/1.1/en/database.arr

Already been through it. No answer inside.

Did you try something like this




public function relations()

{

  return array(

    'sender'=>array(self::BELONGS_TO, 'User', 'PM_From'),

    'recipient'=>array(self::BELONGS_TO, 'User', 'PM_To'),

  );

}



And a query similar to this




$models = PM::model()->with('sender', 'recipient')->findAll();

or

$models = PM::model()->with('sender', 'recipient')->together()->findAll();



(not tested)

/Tommy

It works. Thnx.

This was not required in Yii 1.0

See info box near the top of this page:

http://www.yiiframework.com/doc/guide/1.0/en/database.arr

This was changed in 1.1:

http://www.yiiframework.com/doc/guide/1.1/en/database.arr

Tell us more about what kind of problem you experience?

Are you able to display all records using findAll()?

/Tommy

Edit:

Obviously you solved it while I composed this message.