HABTM relationship as meta information #486

jails opened this Issue May 25, 2012 · 2 comments


None yet
3 participants

jails commented May 25, 2012

I saw a lot of people who getting stuck on HABTM relations.

On IRC @Zebedeu comes with a classic HABTM configuration :

    // Users model
    public $hasMany = array('UsersDoctors');
    // Doctors model
    public $hasMany = array('UsersDoctors');
    // UsersDoctors model
    public $belongsTo = array('Doctors', 'Users');

And expected the following work :

    // To fetch data
    'conditions' => array('Users.id' => 2)
    'with' => 'Doctors'

I think it's a reasonnable request for a "JOIN" compatible datasource.

HABTM is a bad abstraction but what about an HABTM relationship which contain only meta informations.

For exemple by adding to the previous exemple the meta information on how to reach Doctors from Users with the following rule in Users:

$hasAndBelongsToMany = array('Doctors' => array('via' => 'UsersDoctors'));

This way it's not a traditionnal HABTM relation since it can't work on its own and can't work without the "real" hasMany.belongsTo relations. Here it's just aditionnal meta informations to allow the datasources how to process the joins.

And i think it's hard for newcomer to bypass this difficulty at once.

Share your thought.


hans-d commented May 25, 2012

I can imagine cases i also want some subinfo that is also not directly linked to the main model, but only via a submodel, or even further down.
It could use the same idea, but perhaps a more generic variable name is needed, eg $reachable.


nateabele commented May 25, 2012

I really think its better just to support nested relationships. It's more flexible and powerful, because it's a better abstraction.

jails closed this May 25, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment