Getting the value of Primary Key




=== Group ===

id

member_id

description


=== Member ===

id

firstname

lastname



Now I am going to define Member in Groups controller. So there I have defined the Member as like




$members = Members::model()->findAllByPk(array('id','condition'=>'Group=member_id','params'=>array('member_id'=>$member)));



But it is not working and showing error


Trying to get property of non-object.Can someone tell me how to make that condition satisfy.

Not sure what you are after with your approach but this can be effectively accomplished with relations.

Something like:


'group' => array(self::BELONGS_TO, 'Group', 'GroupId'),  //in member

//or

'member' => array(self::HAS_MANY, 'Member', 'GroupId'),  // in group

Thanks @enfield for your reply.Ok I got the relation here. But I want to know what should be the condition to get the value of member?

How could you use such condition on Member table if it doesn’t even have such field?

He’s working in the group controller isn’t he? So if he’s specified a one-to-many relationship, he should just be able to iterate through that relationship.

Yes I am working in Group controller and I have specified one to many relationship in that. Now I want the proper condition in actionUpdate().

Building on the relations example above… once you have relations setup you can do something like this:




$group = Group::model()->find(); //get one record using one of the find() methods


echo $group->member->id


$groups = Group::model()->findAll(): //get all groups matching criteria you pass to findAll()


foreach $groups as $group

    foreach $group->member AS $member

        echo $member->id



I took a look at your code. Why have memberId in the group table? Wouldn’t you rather have groupId in the member table?

If you have a situation where one group HAS MANY members your tables should look like this:

Member:

id

name

groupId

Group:

id

name