Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakePHP, multiple hasOne in a single model

Tags:

php

cakephp

say you have 3 models : user, hair_color, and eye_color

user hasOne hair_color user also hasOne eye_color

however

var $hasOne = 'hair_color';
var $hasOne = 'eye_color';

obviously wont work. So how do you implement many hasOne relationships in a single model?

I assume the answer is in the cookbook, Im going over that area now, I suspect it has something to do with passing an array to $hasOne, but no example of doing this.

like image 854
jason Avatar asked Jan 12 '11 19:01

jason


1 Answers

var $hasOne = array('HairColor', 'EyeColor'); 
// hasOne is an array of model names **not file names**

OR

var $hasOne = array(
    'HairColor' => array(
        'className'    => 'HairColor',
         ...
    ),
    'EyeColor' => array(
        'className'    => 'EyeColor',
         ...
    )
);

You should read manual http://book.cakephp.org/view/80/hasOne

like image 51
Ish Avatar answered Sep 21 '22 14:09

Ish