我已经定义了以下关联: p>
class RecipesTable扩展了Table
{
$ this-> belongsToMany('Ingredients',[
'到'=>'RecipesIngredients',
'externalKey'=>'recipe_id',
'targetForeignKey'= >'ingredient_id',
]);
class IngredientsTable扩展表
{
$ this-> belongsToMany('Recipes',[
'到'=>'RecipesIngredients',
' foreignKey'=>'ingredient_id',
'targetForeignKey'=>'recipe_id',
]);
class RecipesIngredientsTable扩展表
{
$ this-> belongsTo('Recipes');
$ this-> belongsTo('Ingredients');
$ this-> belongsTo('Units');
code> pre>
表'RecipesIngredients' 具有以下结构: p>
id | recipe_id | ingredient_id | unit_id | ...
code> pre>
现在我发出如下所示的请求以获取食谱和相关成分。 但没有单位。
p>
$ data = $ this-> Recipe-> find('all')
- > where('Recipe.id '=> 55)
- >包含(['成分',...])
- >所有();
code> pre>
我的 问题是:如何在 $ this-> Recipe code>的调用中获取相关“单位”的数据? p>
我尝试了不同的包含类似< code> - &gt; contains(['Ingredient'=&gt; ['Unit'],...]) code>(等等),但这不起作用。 CakePHP只返回关联的 ingredients code>和'through'连接表的内容,而不链接到关联的 units code>。 或者给出错误关联的错误。 p>
div>