refactored ingredient model
This commit is contained in:
parent
ee7be3d7e2
commit
f70465a17a
@ -2,6 +2,68 @@
|
||||
|
||||
final class IngredientModel
|
||||
{
|
||||
public $I_INGREDIENT_ID;
|
||||
public $I_RECIPE_ID;
|
||||
public $S_NAME;
|
||||
public $S_QUANTITY;
|
||||
|
||||
public function __construct($I_RECIPE_ID, $S_NAME, $S_QUANTITY)
|
||||
{
|
||||
$this->I_RECIPE_ID = $I_RECIPE_ID;
|
||||
$this->S_NAME = $S_NAME;
|
||||
$this->S_QUANTITY = $S_QUANTITY;
|
||||
}
|
||||
|
||||
private function createFromRow($A_row, $I_id)
|
||||
{
|
||||
$O_ingr = new IngredientModel($A_row["RECIPE_ID"], $A_row["NAME"], $A_row["QUANTITY"]);
|
||||
$O_ingr = $I_id;
|
||||
return $O_ingr;
|
||||
}
|
||||
|
||||
public function insert(){
|
||||
$O_model = Model::get();
|
||||
$stmt = $O_model->prepare("SELECT 1 FROM INGREDIENT WHERE :name=name");
|
||||
$stmt->bindParam("name", $this->S_NAME);
|
||||
$stmt->execute();
|
||||
if($stmt->rowCount() === 0){
|
||||
$stmt = $O_model->prepare("INSERT INTO INGREDIENT (NAME) VALUES(:name)");
|
||||
$stmt->bindParam("name", $this->S_NAME);
|
||||
$stmt->execute();
|
||||
$this->I_INGREDIENT_ID = Model::get()->lastInsertId();
|
||||
}
|
||||
$stmt = $O_model->prepare("INSERT INTO RECIPE_INGREDIENT VALUES(:recipe_id, :ingredient_id, :quantity)");
|
||||
$stmt->bindParam("recipe_id", $this->I_RECIPE_ID);
|
||||
$stmt->bindParam("ingredient_id", $this->I_INGREDIENT_ID);
|
||||
$stmt->bindParam("quantity", $this->S_QUANTITY);
|
||||
$stmt->execute();
|
||||
}
|
||||
|
||||
|
||||
public function delete(){
|
||||
$O_model = Model::get();
|
||||
$stmt = $O_model->prepare("DELETE FROM INGREDIENT WHERE ID=:id");
|
||||
$stmt->bindParam("id", $this->I_INGREDIENT_ID);
|
||||
$stmt->execute();
|
||||
$stmt = $O_model->prepare("DELETE FROM RECIPE_INGREDIENT WHERE INGREDIENT_ID=:id");
|
||||
$stmt->execute();
|
||||
$stmt->bindParam("id", $this->I_INGREDIENT_ID);
|
||||
$stmt->execute();
|
||||
}
|
||||
public static function getByRecipeAndName($I_recipe_id, $S_name){
|
||||
$S_name = strtolower($S_name);
|
||||
$O_model = Model::get();
|
||||
$stmt = $O_model->prepare("SELECT * FROM INGREDIENT
|
||||
JOIN RECIPE_INGREDIENT RI on INGREDIENT.ID = RI.INGREDIENT_ID
|
||||
WHERE NAME=:name");
|
||||
$stmt->bindParam("name", $S_name);
|
||||
$stmt->execute();
|
||||
|
||||
$row = $stmt->fetch();
|
||||
if ($row === false) return null;
|
||||
|
||||
return self::createFromRow($row, $I_recipe_id);
|
||||
}
|
||||
|
||||
public static function searchByRecipe($I_recipe_id)
|
||||
{
|
||||
@ -14,6 +76,13 @@ final class IngredientModel
|
||||
$stmt->bindParam("recipe_id", $I_recipe_id);
|
||||
$stmt->execute();
|
||||
|
||||
return $stmt->fetchAll();
|
||||
$A_users = array();
|
||||
foreach($stmt->fetchAll() as $row){
|
||||
array_push($A_users, self::createFromRow($row, $row["ID"]));
|
||||
}
|
||||
|
||||
return $A_users;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -10,7 +10,7 @@
|
||||
<label for="email">Entrez votre email</label>
|
||||
<input type="email" name="email" id="email" placeholder="Email" required>
|
||||
<label for="password">Entrez votre mot de passe</label>
|
||||
<input type="password" name="password" id="password" placeholder="Mot de passe" required>
|
||||
<input type="password" name="password" id="passzword" placeholder="Mot de passe" required>
|
||||
<input type="hidden" name="return_uri" value="<?= $A_view["return_uri"] ?>">
|
||||
<input type="submit" value="Envoyer">
|
||||
</form>
|
||||
|
Loading…
Reference in New Issue
Block a user