/** * This method allows to delete relationships for a pizza to ingredients * @param integer id the id of the pizza */ public function actionDeleteLinkIngredient($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 { $pizza_ingredient = PizzaIngredient::find()->where(['pizza_id' => $pizza->id, 'ingredient_id' => $ingredient->id]); if ($pizza_ingredient) { $pizza_ingredient->delete(); } } } }
/** * Defines the relationship with the pizzas */ public function getPizza() { return $this->hasOne(Pizza::className(), ['id' => 'pizza_id']); }
/** * Defines the relationship with the pizzas */ public function getPizzas() { return $this->hasMany(Pizza::className(), ['id' => 'pizza_id'])->via('pizzaIngredients'); // the relationship needs the junction table to work }