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
PRO
May 05, 2011
Programming
0
30
Solutions avec Doctrine 2.0
Anna Filina
PRO
May 05, 2011
Tweet
Share
More Decks by Anna Filina
See All by Anna Filina
Semi-Automated Refactoring and Upgrades with Rector
afilina
PRO
0
79
Better Code Design in PHP
afilina
PRO
1
490
Effortless Software Development
afilina
PRO
1
210
Writing Testable Symfony Apps
afilina
PRO
0
270
Upgrading Legacy to the Latest PHP Version
afilina
PRO
0
260
Avoid Costly Framework Upgrades
afilina
PRO
2
1.5k
Fantastic Bugs and How to Avoid Them
afilina
PRO
1
970
Adding Tests to Untestable Legacy Code
afilina
PRO
4
1k
You Don't Need More Developers
afilina
PRO
5
1.4k
Other Decks in Programming
See All in Programming
Try creating your own orderedmap
kazamori
1
180
StoreKit2によるiOSのアプリ内課金のリニューアル
kangnux
0
120
Ruby GitHub Packages
bkuhlmann
0
640
Kotlin Multiplatform at Stable and Beyond (Android Makers 2024)
zsmb
0
470
Behind VS Code Extensions for JavaScript / TypeScript Linnting and Formatting
unvalley
6
1.2k
障害対応を起点としたもっといい開発と運用のサイクル作りのためにできること / Hatena Enginner Seminar #29
polamjag
0
380
雑に思考を整理する技術と効能
konifar
63
30k
Milestoner
bkuhlmann
1
410
Go製Webアプリケーションのエラーとの向き合い方大全、あるいはやっぱりスタックトレース欲しいやん / Kyoto.go #50
utgwkk
6
1.8k
Polars入門
daikikatsuragawa
1
170
大規模UIKitベースアプリへのTCAの段階的導入/gradual-adoption-of-tca-in-a-large-scale-uikit-based-app
takehilo
2
200
『Railsオワコン』と言われる時代に、なぜブルーモ証券はRailsを選ぶのか
free_world21
1
360
Featured
See All Featured
Writing Fast Ruby
sferik
622
60k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
245
20k
Code Review Best Practice
trishagee
56
15k
Building Applications with DynamoDB
mza
88
5.6k
Building a Modern Day E-commerce SEO Strategy
aleyda
21
6.4k
Creatively Recalculating Your Daily Design Routine
revolveconf
211
11k
Art, The Web, and Tiny UX
lynnandtonic
290
19k
Git: the NoSQL Database
bkeepers
PRO
423
63k
A Tale of Four Properties
chriscoyier
152
22k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
352
28k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
275
13k
Product Roadmaps are Hard
iamctodd
45
9.7k
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