Sunday, January 30, 2022

[FIXED] Relational Active record with BELONGS_TO in Yii

Issue

I have 2 tables called Member and MemberResume.

MemberResume references Member on the key memberid.

In the MemberResume model the relation is set like this:

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

I am trying to create a model in this manner.

$model=Memberresume::model()->with('member')->findAllByAttributes(array('memberid'=>$id));

But in the model I am not able to access the attributes of member table like membername etc., though the relational query generated seems to consider the relationship.

Any idea why?


Solution

Try this instead:

$model=Memberresume::model()->findAllByAttributes(
  array('memberid'=>$id), // $attributes
  array('with'=>'member') // $condition (string, array or Criteria object, I think)
);

findAllByAttributes accepts a second "condition" parameter you can add your "with" clause to. Doing it this way should join the Member table so you can access it's attributes.



Answered By - thaddeusmt

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.