Suppose (for lack of better example) I have a model Person and another model Twin (meaning a pair of twins). Twins has two Person foreign keys, say first_born_id and second_born_id referencing the id fields of two different people (in Person). How do I set up the relationships in cake?
I guess Twin would have something like:
$belongsTo = array('FirstBorn' => array('className' => 'Person',
'foreignKey' => 'firstborn_id'),
'SecondBorn' => array('className' => 'Person',
'foreignKey' => 'secondborn_id'));
But how should I set up Person? I could do it like:
$hasOne = array('TwinAsFirstborn' => array('className' => 'Twin',
'foreignKey' => 'firstborn_id'),
'TwinAsSecondborn' => array('className' => 'Twin',
'foreignKey' => 'secondborn_id'));
But then when I have a Person and I want to know about its twin I would need to check both relationships. I guess I am hoping there is a way to get a “Twin” relationship in Person representing any of the two ways a Person can be in a Twin relationship.
Or is there a better way to set this up?
I agree with the Twin example being a little confusing. Let me assume that you have a
Productmodel (Twin in your example) and it always has 2Componentmodels attached to it.I would set up Product as follows:
And Component as:
Basically, you should replace your
hasOnewithhasMany. Each component hasMany products where it is the first component. At the same time, it hasMany products where it is the second component. Hope that makes things clear.EDIT 1: (Oh, and “ProductWithComponentAsComponent#” is just for the purpose of explanation. You can keep whatever short, sweet alias you’d like depending on your actual models.)
EDIT 2: Simple rule of thumb for using hasOne relationships – Use it only when you’re splitting a single table into many (like User/Profile)
EDIT 3: If you want all Products for a Component, then you can do this in two ways.
(A) Define the foreignKey as
falsein your hasMany relation.(B) If the above method does not work (Cake acts weird now and then), you can also use a join to make it obey. Create a function in the Component model like so: