Solutions avec Doctrine 2.0

1,419 views
1,257 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,419
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Solutions avec Doctrine 2.0

  1. 1. Solutions avec Doctrine 2.0PHP Québec, 5 mai 2011
  2. 2. Anna FilinaPHP Québec - groupe d’utilisateursConFoo - conférence sans but lucratifFooLab - solution TI pour entreprises
  3. 3. ÉtendueORM... De Kessé???SetupCommandes de baseCas d’utilisation en entreprise Gestion de rôles API
  4. 4. Pourquoi un ORM?Abstraction du SQLStandardiserSimplifierRéutilisation et extensibilité
  5. 5. Setup
  6. 6. Entitésclass Invoice{ public $id; public $items; public $total;}
  7. 7. Entités: table/** * @Entity * @Table(name="invoice") */class Invoice {
  8. 8. Entités: colonne/** * @Id * @Column(type="integer") * @GeneratedValue */public $id;
  9. 9. Bootstrap: driver$connectionOptions = array( driver => pdo_sqlite, path => database.sqlite);
  10. 10. Schéma>./doctrine orm:schema-tool:create
  11. 11. Commandes de base
  12. 12. Persistance$entityManager =EntityManager::create($conn, $config);$invoice = new Invoice();$invoice->total = 35.00;$entityManager->persist($invoice);$entityManager->flush();
  13. 13. Persistance avecrelations$invoice = new Invoice();$item = new Item();$item->name = Unicorn;$invoice->items[] = $item;$entityManager->persist($invoice);$entityManager->flush();
  14. 14. Relation dans l’entité/** * @ManyToMany(targetEntity="Item", * cascade={"persist"}) */public $items;
  15. 15. Requêtes$query = $entityManager->createQuery( SELECT inv FROM Invoice inv);$invoices = $query->getResult();
  16. 16. Requêtes: left join$query = $entityManager->createQuery( SELECT inv, item FROM Invoice inv LEFT JOIN inv.items item);$invoices = $query->getResult();
  17. 17. Requêtes: left join arrayid => 284total => 0items => array 0 => array id => 279 name => Rainbow price => 10 quantity => 3 1 => array id => 280 name => Unicorn price => 15 quantity => 2
  18. 18. Cas d’utilisation:Gestion de rôles
  19. 19. RôlesAdmin: modifier toutAuthor: modifier projets de la compagnieReader: voir projets publiés seulement
  20. 20. FiltresAdmin: *Author: WHERE project.organisation_id = ?Reader: WHERE project.organisation_id = ? AND project.status = “Published”
  21. 21. Authorclass Author implements Role { public function filterQuery(&$q) { $a = $q->getRootAlias(); $q->add(where, $a..organisation_id = :org_id); $q->setParameter(org_id, $this->org_id); }}
  22. 22. Readerclass Reader implements Role { public function filterQuery(&$q) { $a = $q->getRootAlias(); $q->add(where, $a..organisation_id = :org_id); $q->setParameter(org_id, $this->org_id); $q->add(where, $a..status = "Published"); }}
  23. 23. Cas d’utilisation:API
  24. 24. APIBut: via AJAX, récupérer des données du serveuret injecter dans une pageInput: critères de recherche pour une listeOutput: liste filtrée et paginée
  25. 25. Javascript$.ajax({ url: /project.json, data: { page: 0, pageSize: 10 } success: function(json) { refreshHtmlTable(json); }});
  26. 26. PHPclass API { public function process($entity, $filters) { $q = $entity->getBaseQuery(); $q->setMaxResults($filters[pageSize]); $q->setFirstResult( $filters[pageSize] * $filters[page]); $result = $q->getQuery() ->getResult(Query::HYDRATE_ARRAY); return json_encode($result); }}
  27. 27. Bonus Level!
  28. 28. Combinaisonsclass API { public function process($entity, $filters) { // [...] $this->user->role->filterQuery($q); $q->getQuery()->useResultCache(true, 3600, uid); // [...] }}
  29. 29. http://www.doctrine-project.orghttp://annafilina.com

×