Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Solutions avec Doctrine 2.0
Search
Anna Filina
May 05, 2011
Programming
0
37
Solutions avec Doctrine 2.0
Anna Filina
May 05, 2011
Tweet
Share
More Decks by Anna Filina
See All by Anna Filina
Upgrading Legacy to the Latest PHP Version
afilina
1
120
Better Code Design in PHP
afilina
0
250
Semi-Automated Refactoring and Upgrades with Rector
afilina
0
160
Better Code Design in PHP
afilina
1
420
Better Code Design in PHP
afilina
0
570
Adding Tests to Untestable Legacy Code
afilina
0
360
Upgrading Legacy to the Latest PHP Version
afilina
0
380
Semi-Automated Refactoring and Upgrades with Rector
afilina
0
280
Better Code Design in PHP
afilina
1
740
Other Decks in Programming
See All in Programming
Namespace and Its Future
tagomoris
6
700
Android 16 × Jetpack Composeで縦書きテキストエディタを作ろう / Vertical Text Editor with Compose on Android 16
cc4966
2
230
「手軽で便利」に潜む罠。 Popover API を WCAG 2.2の視点で安全に使うには
taitotnk
0
860
Kiroで始めるAI-DLC
kaonash
2
590
チームのテスト力を鍛える
goyoki
3
190
複雑なフォームに立ち向かう Next.js の技術選定
macchiitaka
2
140
為你自己學 Python - 冷知識篇
eddie
1
350
Performance for Conversion! 分散トレーシングでボトルネックを 特定せよ
inetand
0
860
rage against annotate_predecessor
junk0612
0
170
Introducing ReActionView: A new ActionView-compatible ERB Engine @ Rails World 2025, Amsterdam
marcoroth
0
690
ProxyによるWindow間RPC機構の構築
syumai
3
1.2k
モバイルアプリからWebへの横展開を加速した話_Claude_Code_実践術.pdf
kazuyasakamoto
0
330
Featured
See All Featured
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
229
22k
Designing Experiences People Love
moore
142
24k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.9k
Building an army of robots
kneath
306
46k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
15k
Imperfection Machines: The Place of Print at Facebook
scottboms
268
13k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Producing Creativity
orderedlist
PRO
347
40k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.6k
Why You Should Never Use an ORM
jnunemaker
PRO
59
9.5k
Transcript
Solutions avec Doctrine 2.0 PHP Québec, 5 mai 2011
Anna Filina PHP Québec - groupe d’utilisateurs ConFoo - conférence
sans but lucratif FooLab - solution TI pour entreprises
Étendue ORM... De Kessé??? Setup Commandes de base Cas d’utilisation
en entreprise Gestion de rôles API
Pourquoi un ORM? Abstraction du SQL Standardiser Simplifier Réutilisation et
extensibilité
Setup
Entités class Invoice { public $id; public $items; public $total;
}
Entités: table /** * @Entity * @Table(name="invoice") */ class Invoice
{
Entités: colonne /** * @Id * @Column(type="integer") * @GeneratedValue */
public $id;
Bootstrap: driver $connectionOptions = array( 'driver' => 'pdo_sqlite', 'path' =>
'database.sqlite' );
Schéma >./doctrine orm:schema-tool:create
Commandes de base
Persistance $entityManager = EntityManager::create($conn, $config); $invoice = new Invoice(); $invoice->total
= 35.00; $entityManager->persist($invoice); $entityManager->flush();
Persistance avec relations $invoice = new Invoice(); $item = new
Item(); $item->name = 'Unicorn'; $invoice->items[] = $item; $entityManager->persist($invoice); $entityManager->flush();
Relation dans l’entité /** * @ManyToMany(targetEntity="Item", * cascade={"persist"}) */ public
$items;
Requêtes $query = $entityManager->createQuery( 'SELECT inv FROM Invoice inv'); $invoices
= $query->getResult();
Requêtes: left join $query = $entityManager->createQuery( 'SELECT inv, item FROM
Invoice inv LEFT JOIN inv.items item'); $invoices = $query->getResult();
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
Cas d’utilisation: Gestion de rôles
Rôles Admin: modifier tout Author: modifier projets de la compagnie
Reader: voir projets publiés seulement
Filtres Admin: * Author: WHERE project.organisation_id = ? Reader: WHERE
project.organisation_id = ? AND project.status = “Published”
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); } }
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"'); } }
Cas d’utilisation: API
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
Javascript $.ajax({ url: '/project.json', data: { page: 0, pageSize: 10
} success: function(json) { refreshHtmlTable(json); } });
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); } }
Bonus Level!
Combinaisons class API { public function process($entity, $filters) { //
[...] $this->user->role->filterQuery($q); $q->getQuery()->useResultCache(true, 3600, 'uid'); // [...] } }
http://www.doctrine-project.org http://annafilina.com