I set up a whole bunch of similar (but different) belongsTo relationships in my Items Table, which reference color names from a colors table (in order to get hex values corresponding to those names back). The name column in the colors table has a Unique index on it.
Basically, each item uses several colors from an established palette and I want to select from and display those colors on my page according to their name.
$this->belongsTo('Color1', [
'className' => 'Colors',
'foreignKey' => 'color_1',
'bindingKey' => 'name',
'propertyName' => 'Color1'
]
);
$this->belongsTo('Color2', [
'className' => 'Colors',
'foreignKey' => 'color_2',
'bindingKey' => 'name',
'propertyName' => 'Color2'
]
);
$this->belongsTo('Color3', [
'className' => 'Colors',
'foreignKey' => 'color_3',
'bindingKey' => 'name',
'propertyName' => 'Color3'
]
);
Then, in my controller, I have
$items = $this->Item->findByUName($uname, [
'contain' => ['Color1','Color2','Color3']
])->first();
I’m not sure what I’m doing wrong, but the contain never happens and I’m not getting any errors. I get all my Item data, as well as the color names that are encoded in the associated Item columns… but the SQL log shows no joins, and there are no hex values or anything else coming back.