I’m happy

Model relationships

I can only see belongsTo, hasMany, and HABTM. Would be nice if there was support for hasOne as well.

Pretty interesting concept though. I'm definitely going to play around with this some more.
1 person likes
this idea
+1
Reply
  • EMPLOYEE
    I’m unsure
    When I was developing cakeapp I also thought about this feature, but hasOne does not make sense to implement.

    hasOne: the other model contains the foreign key.
    belongsTo: the current model contains the foreign key.

    So if you want a "hasOne" relationship you can define it as belongsTo in the other table. ... is this correct?
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned sad, anxious, confused, frustrated happy, confident, thankful, excited

  • I’m thankful
    That was my initial thought too, but when I set Table B to belongsTo Table A, it automatically sets Table A to hasMany Table B.

    I'm doing users and profiles here, so each user only needs one profile. But I want to be able to include the profile data when I use the users controller, hence I need a hasOne relationship to the profile model.

    It's my understanding that a belongsTo won't let me include the profile data in a users controller find(). But rather it's the other way around, so it allows me to include user data in a profile controller find().

    It'd be great to have, but honestly I could just to through the code afterwards and change the hasMany to a hasOne. I have a lot of models to do, and I think I only have two hasOne relationships.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned sad, anxious, confused, frustrated happy, confident, thankful, excited

  • EMPLOYEE
    I’m excited
    Yes it is a little bit confusing. The problem is: the DB schema of hasOne and belogsTo is exactly the same! There is no difference in the table structure between a hasOne and belongsTo relationship. Therefore I support only belongsTo. To switch to hasOne, you need to change the model class, not the DB structure.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned sad, anxious, confused, frustrated happy, confident, thankful, excited

  • I’m impressed
    Wow, super quick reply.

    I should have been more specific, sorry. The database schema is identical yes, but the model code is not. I would've liked to see it generate hasOne relationships in addition to hasMany and belongsTo. I've only got two hasOne relationships, but if I had more of them it might get a little tedious to manually have to change them all.

    So instead of doing this:


    class user extends AppModel {

    var $name = 'user';

    //The Associations below have been created with all possible keys, those that are not needed can be removed
    var $hasMany = array(
    'Profile' => array(
    'className' => 'Profile',
    'foreignKey' => 'user_id',
    'dependent' => false,
    'conditions' => '',
    'fields' => '',
    'order' => '',
    'limit' => '',
    'offset' => '',
    'exclusive' => '',
    'finderQuery' => '',
    'counterQuery' => ''
    )
    );

    }


    It'll do this:


    class user extends AppModel {

    var $name = 'user';

    //The Associations below have been created with all possible keys, those that are not needed can be removed
    var $hasOne = array(
    'Profile' => array(
    'className' => 'Profile',
    'foreignKey' => 'user_id',
    'dependent' => false,
    'conditions' => '',
    'fields' => '',
    'order' => '',
    'limit' => '',
    'offset' => '',
    'exclusive' => '',
    'finderQuery' => '',
    'counterQuery' => ''
    )
    );

    }


    Does that sound reasonable, or am I just being silly?
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned sad, anxious, confused, frustrated happy, confident, thankful, excited

  • EMPLOYEE
    I’m indifferent
    Sure I understand what you need. But at the moment CakeApp offers only the ability to change the DB structure and everything else is created automatically with the build in bake and scaffold functions.

    Also if you use the bake shell and use "bake all" then a DB schema which could be a hasOn or belongsTo will always be created as belongsTo.

    In the moment I don't have any idea how to add this feature by changing something in the SQL editor.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned sad, anxious, confused, frustrated happy, confident, thankful, excited

  • I’m happy
    Ah, I see.

    I thought you were doing some magic when you were baking it too, but if all CakeApp does is make the DB schema and then CakePHP bakes it as usual then I understand how you can't do anything about it.

    Thanks for your help though!
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned sad, anxious, confused, frustrated happy, confident, thankful, excited