Craig A Wed Aug 10 09:01:11 -0400 2011

Subject: Multiple self-referencing associations

Hello all, I have come unstuck trying to implement an association, any opinions welcome.

A person has both a mother and father, but when they have children the association would (read should) depend on the gender of the person (being the parent) and use the relevant parent id of any children (i.e. father_id for male parent etc).
I had hoped to have a single has_many association named children but I have no idea how to make the association dependant on an attribute. Is it even possible to do so, or must I just stick with two children associations and have some logic somewhere to decide which association to use at run time?

PEOPLE (DB)
person_id smallint
mother_id smallint
father_id smallint
gender varchar (male/female)

static $has_many = array(
array('m_children', 'foreign_key' => 'mother_id',
'class_name' => 'Person')
array('f_children', 'foreign_key' => 'father_id',
'class_name' => 'Person')
);
static $belongs_to = array(
array('mother', 'foreign_key' => 'mother_id',
'class_name' => 'Person'),
array('father', 'foreign_key' => 'father_id',
'class_name' => 'Person')
);

Clay vanSchalkwijk Tue Sep 13 23:42:34 -0400 2011

I would just set it up as as parent to children and use a getter to parse out the actual relationship since it will be pretty small looping through it wouldn't be much of an issue. get_daughters() would loop through all the children and only return the ones that are female.

What you're trying to do with your method requires a composite key which ar does not support.

(1-1/1)