<?php
class Ingredient { private $pdo; public function __construct(){ $this->pdo=$GLOBALS['pdo']; }
  public function all(){ return $this->pdo->query("SELECT * FROM ingredients ORDER BY name ASC")->fetchAll(); }
  public function find($id){ $st=$this->pdo->prepare("SELECT * FROM ingredients WHERE id=?"); $st->execute([$id]); return $st->fetch(); }
  public function create($d){ $st=$this->pdo->prepare("INSERT INTO ingredients (name,unit,supplier,notes) VALUES (?,?,?,?)"); return $st->execute([$d['name'],$d['unit']??'', $d['supplier']??'', $d['notes']??'']); }
  public function update($id,$d){ $st=$this->pdo->prepare("UPDATE ingredients SET name=?, unit=?, supplier=?, notes=? WHERE id=?"); return $st->execute([$d['name'],$d['unit']??'', $d['supplier']??'', $d['notes']??'', $id]); }
  public function delete($id){ $st=$this->pdo->prepare("DELETE FROM ingredients WHERE id=?"); return $st->execute([$id]); }
}
