第1引数はアソシエーションの名前です。そのまま Theme エンティティーのフィールド名になります(ただし lower_snake 単数形)。
[ThemesTable]
php
1$this->belongsTo('StaffsTemplates', [
2 'foreignKey' => 'staffs_template_id',
3 'className' => 'Templates',
4 'joinType' => 'INNER'
5]);
6$this->belongsTo('TalksTemplates', [
7 'foreignKey' => 'talks_template_id',
8 'className' => 'Templates',
9 'joinType' => 'INNER'
10]);
[ThemesController]
php
1$theme = $this->Themes->get($id, [
2 'contain' => ['StaffsTemplates', 'TalksTemplates'] // アソシエーション名を指定
3]);
4debug($themes); /*
5object(App\Model\Entity\Theme) {
6 'id' => (int) 1,
7 'staffs_template_id' => (int) 2,
8 'talks_template_id' => (int) 1,
9 'talks_template' => object(App\Model\Entity\Template) {
10 'id' => (int) 1,
11 },
12 'staffs_template' => object(App\Model\Entity\Template) {
13 'id' => (int) 2,
14 },
15}
16*/// …みたいな構造が返ってくる
ちなみに cake 上で変数を出すなら debug 関数が超絶便利です。(layoutなどの環境にもよりますが)
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/03/23 14:30
2016/03/24 01:02