Hi I am building my User model to have only one profile picture, which is an example of an Image model. I want the conditions of the hasOne to only include the image where the Image.id is equal to the "picture" column in the user table. I know there is something wrong...here is the relevant part of code.
var $hasOne = array(
'ProfileImage' => array(
'className' => 'Image',
'conditions' => array('ProfileImage.id' => 'User.picture'),
'dependent' => false,
'foreignKey' => 'user_id',
)
);
What is wrong with my conditions? How do I specifc ProfileImage.id to be 开发者_如何学运维equal to the picture column in the User model? Thanks!
If the foreign key (picture
) is in the User model, then your association should be User belongsTo Image. If the image can only be used by one user at a time, the reverse would be Image hasOne user. If an image can be used by multiple users, it would be Image hasMany User.
But, if the image can only be used by one user at a time, the better data structure would be to add a user_id
column to the images
table. Then you have a User hasOne Image/Image belongsTo User relationship.
For neither you need conditions
, only the foreignKey
.
After your clarification of the issue, your condition needs to look like:
'conditions' => array('ProfileImage.id = User.picture')
Otherwise Cake thinks ProfileImage.id
should equal the string "User.picture"
.
精彩评论