【发布时间】:2017-07-18 22:14:33
【问题描述】:
我定义了以下关联:
class RecipesTable extends Table
{
$this->belongsToMany('Ingredients', [
'through' => 'RecipesIngredients',
'foreignKey' => 'recipe_id',
'targetForeignKey' => 'ingredient_id',
]);
class IngredientsTable extends Table
{
$this->belongsToMany('Recipes', [
'through' => 'RecipesIngredients',
'foreignKey' => 'ingredient_id',
'targetForeignKey' => 'recipe_id',
]);
class RecipesIngredientsTable extends Table
{
$this->belongsTo('Recipes');
$this->belongsTo('Ingredients');
$this->belongsTo('Units');
“RecipesIngredients”表具有以下结构:
id | recipe_id | ingredient_id | unit_id | ...
现在我提出如下请求,以获取食谱和相关成分。但没有单位。
$data = $this->Recipe->find('all')
->where('Recipe.id' => 55)
->contain(['Ingredient', ...])
->all();
我的问题是:如何在$this->Recipe 的调用中获取关联“单位”的数据?
我尝试了不同的包含,例如->contain(['Ingredient' => ['Unit'], ...])(等等),但这不起作用。 CakePHP 只返回关联的ingredients 和'through' 连接表的内容,而不链接到关联的units。或者给出缺少关联的错误。
【问题讨论】:
标签: php cakephp cakephp-3.x