Différences
Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| framework-web:symfony:models:crud [2018/02/05 11:01] – créée jcheron | framework-web:symfony:models:crud [2019/08/31 14:21] (Version actuelle) – modification externe 127.0.0.1 | ||
|---|---|---|---|
| Ligne 2: | Ligne 2: | ||
| ===== Entity manager & Repository ===== | ===== Entity manager & Repository ===== | ||
| + | |||
| + | ==== Entity manager ==== | ||
| Récupération de l' | Récupération de l' | ||
| Ligne 11: | Ligne 13: | ||
| Comme indiqué, l' | Comme indiqué, l' | ||
| + | |||
| + | ==== Repository ==== | ||
| + | |||
| + | <sxh php> | ||
| + | $productRepository = $this-> | ||
| + | </ | ||
| + | |||
| + | |||
| + | Pour une bonne séparation du code, il est conseillé de créer des classes Repository pour chacune des classes métier utilisées : | ||
| + | |||
| + | <sxh php; | ||
| + | namespace App\Repository; | ||
| + | //... | ||
| + | class UserRepository extends ServiceEntityRepository { | ||
| + | |||
| + | public function __construct(ManagerRegistry $registry) { | ||
| + | parent:: | ||
| + | } | ||
| + | |||
| + | //... | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | Il est ensuite possible d' | ||
| + | |||
| + | <sxh php; | ||
| + | class UsersController extends Controller{ | ||
| + | /** | ||
| + | * @var UserRepository | ||
| + | */ | ||
| + | private $repository; | ||
| + | |||
| + | public function __construct(UserRepository $userRepo){ | ||
| + | $this-> | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Read : chargement d' | ||
| + | |||
| + | Récupération du Repository : | ||
| + | <sxh php> | ||
| + | $repository = $this-> | ||
| + | </ | ||
| + | |||
| + | Chargement d'une instance (par sa clé primaire, généralement ' | ||
| + | <sxh php> | ||
| + | $product = $repository-> | ||
| + | </ | ||
| + | |||
| + | Chargement d'une instance sur critères variés : | ||
| + | <sxh php> | ||
| + | // query for a single Product by name | ||
| + | $product = $repository-> | ||
| + | // or find by name and price | ||
| + | $product = $repository-> | ||
| + | ' | ||
| + | ' | ||
| + | ]); | ||
| + | </ | ||
| + | |||
| + | Chargement de plusieurs instances | ||
| + | |||
| + | <sxh php> | ||
| + | // query for multiple Product objects matching the name, ordered by price | ||
| + | $products = $repository-> | ||
| + | [' | ||
| + | [' | ||
| + | ); | ||
| + | |||
| + | // find *all* Product objects | ||
| + | $products = $repository-> | ||
| + | </ | ||
| + | |||