phpcakephp

CakePHP Association is not working with userdata to userdatatype


I am currently developing a webapp containing some userdata. I have the following tables:

users - contains basic userdata.
userdata - contains large amount of different information about the user.
userdatatype - contains definition for the different userdata.

In CakePHP I am using the hasMany association in the user model to retrieve the userdata information (which works fine) and in the userdata model I want to use the hasOne association to retrieve the corresponding userdatatype information. But this is not working as userdatatype obviously has no userdata_id as it is the definition for the userdata.

I tried to set the foreignKey to false in the hasOne association and work with the condition 'Userdatatype.id => ??' but I don't know how to access the instance. How do I link this models together without to much overhead in my code? Do I really need to loop through the userdata array and establish the connection by hand?

public $hasOne = array(
    'Userdatatype' => array(
        'className' => 'Userdatatype',
        'foreignkey' => false,
        'conditions' => array('Userdatatype.id' = WHAT DO I SET HERE?)
    )
);

Solution

  • Tables: users, userdatas, userdatatypes.
    
    users -> id, email, name etc.
    userdatas -> id, user_id, data1, data2, data3 etc.
    userdatatypes -> id, userdata_id, type
    
    **Model User.php :** 
    
        public $hasMany = array('Userdata');
    
    **Model Userdata.php :**
    
        public $belongsTo = array('User');
        public $hasOne = array('Userdatatype');
    
    **Model Userdatatype.php :**
    
        public $belongsTo = array('Userdata');
    

    This should link your models together, if i understood the question correctly.