public function update(Request $request, $id) { $ingredient = Ingredient::find($id); $ingredient->fill($request->all()); $ingredient->save(); return $ingredient; }
public static function parseIngredientsFromText($text) { $header = null; $ingredients = []; foreach (preg_split("/((\r?\n)|(\r\n?))/", $text) as $line) { $line = trim($line); if (preg_match('/^###?#? ?[\\w|\\d| ]+/', $line)) { $header = trim(preg_replace('/^###?#?/', '', $line)); } else { $ingredients[] = Ingredient::createFromLine($line, $header); } } return $ingredients; }
/** * Defines the relationship with the ingredients */ public function getIngredient() { return $this->hasOne(Ingredient::className(), ['id' => 'ingredient_id']); }
/** * 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 ingredients */ public function getIngredients() { return $this->hasMany(Ingredient::className(), ['id' => 'ingredient_id'])->via('pizzaIngredients'); // the relationship needs the junction table to work }