Comments: Relationship Types

By MilkyJoe on 4/3/08

1 - Is it worth

The cook book is a great enhancement - i love being able to comment on specific sections, and will edit some sections one day when I'm more confident (and have some time). In the mean time...

Is it worth mentioning that associations come in twos, or at least, can come in twos? and explicitly saying what those twos are. eg. hasOne and belongsTo

The reason I say this is that I'm not that experienced with associations and didn't realise that a hasOne should be complimented with a belongsTo. I was using hasone for both associations and it all went wrong.

Appreciate that you have addressed this in the belongsTo section but I think it is worth highlighting in the introduction and also in the hasOne section.

[the following comment is for the hasOne section]

Let’s set up a User model with a hasOne relationship to a Profile model. After this we will set up a complementary belongsTo relationship.

By teum on 3/6/08

2 - Suggestion

Hi,

I don't know if it is hard to handle, but I think having a "isA" association type would be useful in case of legacy. For example, if you have two types of users described by different informations but who both have a login and a pass. Let's say we have admins and customer. Then, a user < is a(n) > admin OR a user < is a > customer. We could set the different user types in an array.

Example :

class User extends AppModel {

var $name = 'User';

var $isA = array('admin', 'customer');

}

Of course, this could be done using two hasOne associations, but each user would be bound either to an empty admin or an empty customer, depending on its type, which is useless.

By teum on 3/6/08

3 - ???

Or maybe creating a Customer class and an Admin class both extending a User class would solve the problem ? It's not easy to switch between Merise and UML. "hasOne", "hasMany" etc makes me think about Merise, whereas legacy sounds more UML :) (although the legacy relationship exists in Merise). If would be grateful to anyone who could help me solving this problem.