Table not associated with table - ManyToMany

Hello All,

I am VERY new to CakePHP and i am trying to develop my verý first project in it (a personal project, strictly for learning).

I am having problems with a many-to-many relationship with additional join data. Here is the relationships:
A Recipe contains many Ingredients.
An Ingredient can be used in many Recipes.
Therefore, the RecipesIngredients table joins them together. HOWEVER - this table also has an “amount” column to know how much of a particular ingredient is used in a recipe.

I am trying to create a new Recipe, and this is what i have so far:

                // This is an ingredient, make an ingredient entity we can pass to the recipe
                // First we get the id
                $newIngredient = $ingredientsTable->get($id);

                // Set the ingredient amount
                $newIngredient->_joinData = $ingredientsTable->RecipesIngredients->newEntity();
                $newIngredient->_joinData->amount = $value;

                $allIngredients[] = $newIngredient;

        // Set the recipe information
        $recipe->name = $this->request->data('name');
        $recipe->ingredients = $allIngredients;`

This is just a modified snippet to show how i think it should be done. Now, when i try to save, i get this error:
**Table “App\Model\Table\IngredientsTable” is not associated with “RecipesIngredients” **

But as far as i can tell, it is associated with it. Here is my IngredientsTable:
`class IngredientsTable extends Table
{

/**
 * Initialize method
 *
 * @param array $config The configuration for the Table.
 * @return void
 */
public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('ingredients');
    $this->displayField('name');
    $this->primaryKey('id');

    $this->belongsToMany('Recipes', [
        'foreignKey' => 'ingredient_id',
        'targetForeignKey' => 'recipe_id',
        'joinTable' => 'recipes_ingredients',
        'through' => 'recipes_ingredients'
    ]);
}

And as you can see, it is associated with the recipes_ingredients. Also, in the RecipesIngredients table:
`class RecipesIngredientsTable extends Table
{

/**
 * Initialize method
 *
 * @param array $config The configuration for the Table.
 * @return void
 */
public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('recipes_ingredients');
    $this->displayField('recipe_id');
    $this->primaryKey(['recipe_id', 'ingredient_id']);

    $this->belongsTo('Recipes', [
        'foreignKey' => 'recipe_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsTo('Ingredients', [
        'foreignKey' => 'ingredient_id',
        'joinType' => 'INNER'
    ]);
}`

So as far as i can tell, the association is there, so why does it fail when i save it?

Thanks in advance! :slight_smile: