/** * This method allows to create relationships for a pizza to ingredients * @param integer id the id of the pizza */ public function actionCreateLinkIngredient($id) { $pizza = Pizza::find()->where(['id' => $id])->one(); if (!$pizza) { throw new HttpException(404, "Pizza with id:{$id}, Not found."); } $params = Yii::$app->request->bodyParams; foreach ($params["data"] as $array_ingredient) { $ingredient = Ingredient::find()->where(['id' => $array_ingredient["id"]])->one(); if (!$ingredient) { throw new HttpException(404, "Ingredient with id:{$id}, Not found."); } else { if (!isset($array_ingredient["quantity"])) { throw new HttpException(409, "You must enter a quantity when assigning an ingredient to a pizza."); } else { $pizza_ingredient = PizzaIngredient::find()->where(['pizza_id' => $pizza->id, 'ingredient_id' => $ingredient->id]); /*if (!$pizza_ingredient) {*/ $pizza_ingredient = new PizzaIngredient(); $pizza_ingredient->pizza_id = $pizza->id; $pizza_ingredient->ingredient_id = $ingredient->id; $pizza_ingredient->quantity = $array_ingredient["quantity"]; $pizza_ingredient->save(); //} } } } }