Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Solutions avec Doctrine 2.0

Solutions avec Doctrine 2.0

Anna Filina

May 05, 2011
Tweet

More Decks by Anna Filina

Other Decks in Programming

Transcript

  1. Anna Filina PHP Québec - groupe d’utilisateurs ConFoo - conférence

    sans but lucratif FooLab - solution TI pour entreprises
  2. Persistance avec relations $invoice = new Invoice(); $item = new

    Item(); $item->name = 'Unicorn'; $invoice->items[] = $item; $entityManager->persist($invoice); $entityManager->flush();
  3. Requêtes: left join $query = $entityManager->createQuery( 'SELECT inv, item FROM

    Invoice inv LEFT JOIN inv.items item'); $invoices = $query->getResult();
  4. Requêtes: left join array 'id' => 284 'total' => 0

    'items' => array 0 => array 'id' => 279 'name' => 'Rainbow' 'price' => 10 'quantity' => 3 1 => array 'id' => 280 'name' => 'Unicorn' 'price' => 15 'quantity' => 2
  5. Filtres Admin: * Author: WHERE project.organisation_id = ? Reader: WHERE

    project.organisation_id = ? AND project.status = “Published”
  6. Author class 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); } }
  7. Reader class 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"'); } }
  8. API But: via AJAX, récupérer des données du serveur et

    injecter dans une page Input: critères de recherche pour une liste Output: liste filtrée et paginée
  9. Javascript $.ajax({ url: '/project.json', data: { page: 0, pageSize: 10

    } success: function(json) { refreshHtmlTable(json); } });
  10. PHP class 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); } }
  11. Combinaisons class API { public function process($entity, $filters) { //

    [...] $this->user->role->filterQuery($q); $q->getQuery()->useResultCache(true, 3600, 'uid'); // [...] } }