Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
923 lines (791 loc) · 39.9 KB
/
Copy pathindex.html
File metadata and controls
923 lines (791 loc) · 39.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Doctrine 2 ORM and Zend Framework 2 tutorial</title>
<meta name="description" content="A tutorial about integrating Doctrine2 with ZF2 through DoctrineModule">
<meta name="author" content="Marco Pivetta">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="stylesheet" href="reveal.js/css/reveal.min.css">
<link rel="stylesheet" href="reveal.js/css/theme/default.css" id="theme">
<!-- #040E27 -->
<!-- For syntax highlighting -->
<link rel="stylesheet" href="reveal.js/lib/css/zenburn.css">
<!-- If the query includes 'print-pdf', use the PDF print sheet -->
<script>
document.write( '<link rel="stylesheet" href="reveal.js/css/print/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' );
</script>
<!--[if lt IE 9]>
<script src="reveal.js/lib/js/html5shiv.js"></script>
<![endif]-->
<style type="text/css">
/* overrides */
body {
background: #040E27;
background: -moz-radial-gradient(center, circle cover, #555a5f 0%, #040E27 100%);
background: -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(0%, #555a5f), color-stop(100%, #040E27));
background: -webkit-radial-gradient(center, circle cover, #555a5f 0%, #040E27 100%);
background: -o-radial-gradient(center, circle cover, #555a5f 0%, #040E27 100%);
background: -ms-radial-gradient(center, circle cover, #555a5f 0%, #040E27 100%);
background: radial-gradient(center, circle cover, #555a5f 0%, #040E27 100%);
background-color: #040E27;
}
.reveal section img {
border: 0;
background: none;
box-shadow: 0 0 #000;
}
.zf-color {
color: #68B604;
}
.doctrine-color {
color: #FC6A31;
}
.doctrine-bg {
background-color: #040E27;
}
</style>
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>
<span class="doctrine-color">Doctrine 2</span>
&
<span class="zf-color">ZF2</span>
</h1>
<h1>
<img
src="assets/img/doctrine.svg"
alt="Doctrine Project" border="0"
height="200px"
style="margin-bottom: -50px; "
/>
+
<img
src="assets/img/zf-logo-large.png"
alt="Zend Framework 2"
border="0"
height="200px"
style="margin-bottom: -50px;"
/>
</h1>
</section>
<section>
<p>
Note: these slides are combined with a webcast that you can find on
<a href="http://ocramius.github.io/blog/doctrine-2-orm-zf2-tutorial/" target="_blank">
this page
</a>.
</p>
</section>
<section>
<h2>
<img
src="assets/img/ocramius.gif"
alt="Marco Pivetta"
style="width: 50px"
/>
<a href="http://ocramius.github.com/" target="_blank" rel="me">Marco Pivetta</a>
</h2>
<p class="fragment">
<span class="doctrine-color">Doctrine</span> core team
</p>
<p class="fragment">
<span class="zf-color">Zf2</span> contributor
</p>
<p class="fragment">
<span class="zf-color">Modules</span> <strike>developer</strike> <span class="fragment">time waster</span>
</p>
<p class="fragment">
<a href="http://twitter.com/Ocramius" target="_blank">@Ocramius</a>
-
<a href="http://github.com/Ocramius" target="_blank">Ocramius</a>
</p>
</section>
<section>
<h2>Main Libraries</h2>
<p>
<a target="_blank" href="https://github.com/bjyoungblood/BjyAuthorize">BjyAuthorize</a>,
<a target="_blank" href="https://github.com/RWOverdijk/AssetManager">AssetManager</a>,
<a target="_blank" href="https://github.com/BinaryKitten/ZeffMu">ZeffMu</a>,
<a target="_blank" href="https://github.com/zf-fr/ZfrRest">ZfrRest</a>,
<a target="_blank" href="https://github.com/Ocramius/OcraDiCompiler">OcraDiCompiler</a>,
<a target="_blank" href="https://github.com/Ocramius/OcraServiceManager">OcraServiceManager</a>,
<a target="_blank" href="https://github.com/Ocramius/OcraCachedViewResolver">OcraCachedViewResolver</a>,
<a target="_blank" href="https://github.com/doctrine/DoctrineModule">DoctrineModule</a>,
<a target="_blank" href="https://github.com/doctrine/DoctrineORMModule">DoctrineORMModule</a>,
<a target="_blank" href="https://github.com/doctrine/DoctrineMongoODMModule">DoctrineMongoODMModule</a>,
<a target="_blank" href="https://github.com/Ocramius/VersionEyeModule">VersionEyeModule</a>
</p>
</section>
<section>
<h2><span class="doctrine-color">Doctrine Project</span></h2>
<p>
An incubator for <span class="doctrine-color">persistence</span>-oriented libraries
</p>
<p>
<img src="https://secure.gravatar.com/avatar/d07a7a143b14fc8309f9abb78d569344?s=70" alt="asm89"/>
<img src="https://secure.gravatar.com/avatar/f000c9b4dd0656f60de1dc9e75f7386c?s=70" alt="avalanche123"/>
<img src="https://secure.gravatar.com/avatar/75f5fb3ddda052e46f1daed314ae69ab?s=70" alt="beberlei"/>
<img src="https://secure.gravatar.com/avatar/14327ba6a017db91465d34f5910df5ab?s=70" alt="davidino"/>
<img src="https://secure.gravatar.com/avatar/c9f4f8ce2c2936bacaf49500b5a127dc?s=70" alt="dbu"/>
<img src="https://secure.gravatar.com/avatar/847009eb569c152f194f70e2deba99e1?s=70" alt="FabioBatSilva"/>
<img src="https://secure.gravatar.com/avatar/f1a2c5905e121d09feba5ad73898ffca?s=70" alt="fabpot"/>
<img src="https://secure.gravatar.com/avatar/fbfe5a0027befc4b766f5fdc4cd530d8?s=70" alt="golovanov"/>
<img src="https://secure.gravatar.com/avatar/5f258a6128e0e026bf91f7ace0f85967?s=70" alt="guilhermeblanco"/>
<img src="https://secure.gravatar.com/avatar/f23700b51dc0c196c1dc02f84aeeecdf?s=70" alt="jmikola"/>
<img src="https://secure.gravatar.com/avatar/f76041410752f9019752b6afd2bebc2a?s=70" alt="jwage"/>
<img src="https://secure.gravatar.com/avatar/29225b18371c37ec44c7b8831ea3dae3?s=70" alt="kore"/>
<img src="https://secure.gravatar.com/avatar/097f39f4cd9a83a03d92c246a2797880?s=70" alt="kriswallsmith"/>
<img src="https://secure.gravatar.com/avatar/54787afbd0e7c30936101c2fa84bd89b?s=70" alt="lsmith77"/>
<img src="https://secure.gravatar.com/avatar/9f580202b05cc640aa9297ab7a1ae764?s=70" alt="naderman"/>
<img src="https://secure.gravatar.com/avatar/1c29f9b1bf5f1b88ed8b0c9a9be39788?s=70" alt="nrk"/>
<img src="https://secure.gravatar.com/avatar/3cf7a8ab0bccee5b65d39b50a170a555?s=70" alt="Ocramius"/>
<img src="https://secure.gravatar.com/avatar/7e1b44a243d0cd4a01e5de1fa41fc0ed?s=70" alt="odino"/>
<img src="https://secure.gravatar.com/avatar/ae002f3da6a8fb6714653dc49c9284b8?s=70" alt="richardfullmer"/>
<img src="https://secure.gravatar.com/avatar/20f4b48ecceee43353a1ec364cd0059c?s=70" alt="rndstr"/>
<img src="https://secure.gravatar.com/avatar/a459a18ff578f511412e9b78b1c3a7fb?s=70" alt="romanb"/>
<img src="https://secure.gravatar.com/avatar/96a13b96ece78afe3c2dc841edc4a8f5?s=70" alt="schmittjoh"/>
<img src="https://secure.gravatar.com/avatar/48b79d17cd8a911327cbd88c122b1efb?s=70" alt="Seldaek"/>
<img src="https://secure.gravatar.com/avatar/7894bbdf1c05b18a1444ad8c76c9d583?s=70" alt="stof"/>
</p>
</section>
<section>
<h2>What is <span class="doctrine-color">Doctrine ORM</span>?</h2>
<p class="fragment">
<span class="doctrine-color">Doctrine ORM</span> is an Object Relational Mapper
</p>
<p class="fragment">
It is inspired by Hibernate and the <abbr title="Java Persistece API">JPA</abbr> (JSR-317)
</p>
<p class="fragment">
It is based on a
<abbr title="Database Abstraction Layer" class="doctrine-color">DBAL</abbr>
(DataBase Abstraction Layer)
</p>
<p class="fragment">
Allows developers to save and load <abbr title="Plain Old PHP Object">POPO</abbr> with SQL
</p>
<aside class="notes">
Also consider that Doctrine ORM abstract the underlying SQL dialect of the DB platform you are
using, so it's possible for you to switch DB vendor from a day to another
</aside>
</section>
<section>
<p>
An <span class="doctrine-color">ORM</span> gives you the impression that you are working
with a "virtual" database (graph) composed by objects
</p>
</section>
<section>
<p>
Simply put:
</p>
<h2 class="doctrine-color">Forget the database!</h2>
<aside class="notes">
The job of an ORM is all about abstracting the underlying data structure into something that
gives the developer access to what looks like an in-memory representation of the database.
</aside>
</section>
<section>
<h2>
The <span class="zf-color">Modules</span>!
</h2>
<p class="fragment">
<span class="zf-color">DoctrineModule</span>
<br/>
basic common functionality
</p>
<p class="fragment">
<span class="zf-color">DoctrineORMModule</span>
<br/>
ORM/SQL Connection
</p>
<p class="fragment">
<span class="zf-color">DoctrineMongoODMModule</span>
<br/>
ODM/MongoDB Connection
</p>
<aside class="notes">
DoctrineModule is a set of functionality common to all ZF2-Doctrine integration layers
</aside>
</section>
<section data-state="soothe">
<p>
<img src="http://yuml.me/diagram/scruffy;dir:TB;/class/[doctrine/common{bg:orange}]<-.- [doctrine/doctrine-module{bg:green}],[doctrine/orm{bg:orange}]<-.- [doctrine/doctrine-orm-module{bg:green}],[doctrine/mongodb-odm{bg:orange}]<-.- [doctrine/doctrine-mongo-odm-module{bg:green}],[doctrine/doctrine-module{bg:green}]<-.- [doctrine/doctrine-orm-module{bg:green}],[doctrine/doctrine-module{bg:green}]<-.- [doctrine/doctrine-mongo-odm-module{bg:green}],[doctrine/common{bg:orange}]<-.- [doctrine/mongodb-odm{bg:orange}],[doctrine/common{bg:orange}]<-.- [doctrine/orm{bg:orange}]">
</p>
</section>
<section>
<h2>Installation!</h2>
</section>
<section>
<pre><code contenteditable><span class="doctrine-color">php composer.phar</span> require doctrine/doctrine-orm-module:<span class="zf-color">0.7.*</span></code></pre>
</section>
<section>
<img src="assets/img/waiting-meme.png" width="600px" alt="Wait a bit"/>
</section>
<section>
<img src="assets/img/successful-installation.png" alt="Doctrine ORM Module successfully installed!"/>
</section>
<section>
<pre><code contenteditable><span class="doctrine-color">php composer.phar</span> require zendframework/zend-developer-tools:<span class="zf-color">dev-master</span></code></pre>
</section>
<section>
<img src="assets/img/waiting-meme.png" width="600px" alt="Wait a bit more"/>
</section>
<section>
<img src="assets/img/successful-zdt-installation.png" alt="ZendDeveloperTools successfully installed!"/>
</section>
<section>
<pre><code contenteditable>cp vendor/zendframework/zend-developer-tools/config/zenddevelopertools.local.php.dist config/autoload/zdt.local.php</code></pre>
</section>
<section>
<h3>Enabling the modules</h3>
<pre><code>config/application.config.php</code></pre>
<pre><code contenteditable>return array(
'modules' => array(
'ZendDeveloperTools',
'DoctrineModule',
'DoctrineORMModule',
'Application',
),
// [...]
);
</code></pre>
</section>
<section>
<p>You should see:</p>
<p>
<img src="assets/img/toolbar-preview.png" alt="Successful toolbar installation"/>
</p>
</section>
<section>
<h2>Write your first entity</h2>
<pre><code>module/Application/src/Application/Entity/User.php</code></pre>
<pre><code contenteditable>namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
/** @ORM\Entity */
class User {
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $id;
/** @ORM\Column(type="string") */
protected $fullName;
// getters/setters
}</code></pre>
</section>
<section>
<h2>Configure <span class="doctrine-color">mappings</span></h2>
<pre><code>module/Application/config/module.config.php</code></pre>
<pre><code contenteditable>return array(
'doctrine' => array(
'driver' => array(
'application_entities' => array(
'class' =>'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/Application/Entity')
),
'orm_default' => array(
'drivers' => array(
'Application\Entity' => 'application_entities'
)
))), // [...]</code></pre>
</section>
<section>
<p>You should see:</p>
<img src="assets/img/successful-mapping-of-user.png" alt="Successful mapping of the user entity"/>
</section>
<section>
<h2>Configure the <span class="doctrine-color">connection</span></h2>
<pre><code>config/autoload/doctrine.local.php</code></pre>
<pre><code contenteditable>return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' =>'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => 'localhost',
'port' => '3306',
'user' => 'username',
'password' => 'password',
'dbname' => 'database',
)))));</code></pre>
</section>
<section>
<h2>Validate <span class="doctrine-color">mappings</span></h2>
<pre><code contenteditable><span class="zf-color">./vendor/bin/doctrine-module</span> <span class="doctrine-color">orm:validate-schema</span></code></pre>
<p>
<img src="assets/img/validate-mappings.png" alt="Validate schema mappings"/>
</p>
</section>
<section>
<h2><span class="doctrine-color">Generate</span> the database</h2>
<p>
<img src="assets/img/amazed-meme.gif" alt="Generating databases?! WOAH!"/>
</p>
</section>
<section>
<pre><code contenteditable><span class="zf-color">./vendor/bin/doctrine-module</span> <span class="doctrine-color">orm:schema-tool:create</span></code></pre>
<p>
<img src="assets/img/database-generated-successfully.png" alt="generating the database"/>
</p>
</section>
<!-- add slide about mapping overview in ZDT -->
<section>
<h2>Test it!</h2>
<pre><code>module/Application/src/Application/Controller/IndexController.php</code></pre>
<pre><code contenteditable>public function indexAction() {
$objectManager = $this
->getServiceLocator()
->get('Doctrine\ORM\EntityManager');
$user = new \Application\Entity\User();
$user->setFullName('Marco Pivetta');
$objectManager->persist($user);
$objectManager->flush();
die(var_dump($user->getId())); // yes, I'm lazy
}</code></pre>
</section>
<section>
<section>
<h2>Examples</h2>
<img width="178" height="238" src="assets/img/down-arrow.png" alt="Down arrow"/>
</section>
<section>
<h3><span class="doctrine-color">Persisting</span> an object</h3>
<pre><code contenteditable>$user = new User();
$user->setFullName('Marco Pivetta');
$objectManager->persist($user); // $user is now "<span class="doctrine-color">managed</span>"
$objectManager->flush(); // commit changes to db
var_dump($user->getId()); // 1</code></pre>
</section>
<section>
<h3><span class="doctrine-color">Persisting</span> multiple objects</h3>
<pre><code contenteditable>$user1 = new User();
$user1->setFullName('Marco Pivetta');
$objectManager->persist($user1);
$user2 = new User();
$user2->setFullName('Michaël Gallego');
$objectManager->persist($user2);
$user3 = new User();
$user3->setFullName('Kyle Spraggs');
$objectManager->persist($user3);
$objectManager->flush();</code></pre>
</section>
<section>
<h3>Retrieving an object</h3>
<pre><code contenteditable>$user1 = $objectManager->find('Application\Entity\User', 1);
var_dump($user1->getFullName()); // Marco Pivetta
$user2 = $objectManager
->getRepository('Application\Entity\User')
->findOneBy(array('fullName' => 'Michaël Gallego'));
var_dump($user2->getFullName()); // Michaël Gallego</code></pre>
</section>
<section>
<h3>Updating an object</h3>
<pre><code contenteditable>$user = $objectManager->find('Application\Entity\User', 1);
$user->setFullName('Guilherme Blanco');
$objectManager->flush();</code></pre>
</section>
<section>
<h3>Deleting an object</h3>
<pre><code contenteditable>$user = $objectManager->find('Application\Entity\User', 1);
$objectManager->remove($user);
$objectManager->flush();</code></pre>
</section>
<section>
<h3><span class="doctrine-color">Associations</span> - User</h3>
<pre><code contenteditable>/** @ORM\Entity */
class User {
// like before
/** @ORM\ManyToOne(targetEntity="Address") */
protected $address;
/** @ORM\ManyToMany(targetEntity="Project") */
protected $projects;
public function __construct()
{
$this->projects = new ArrayCollection();
}
// getters/setters
}</code></pre>
</section>
<section>
<h3><span class="doctrine-color">Associations</span> - Address</h3>
<pre><code contenteditable>/** @ORM\Entity */
class Address {
/** @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue(strategy="AUTO") */
protected $id;
/** @ORM\Column(type="string") */
protected $city;
/** @ORM\Column(type="string") */
protected $country;
// getters/setters etc.
}</code></pre>
</section>
<section>
<h3><span class="doctrine-color">Associations</span> - Projects</h3>
<pre><code contenteditable>/** @ORM\Entity */
class Project {
/** @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue(strategy="AUTO") */
protected $id;
/** @ORM\Column(type="string") */
protected $name;
// getters/setters
}</code></pre>
</section>
<section>
<h3><span class="doctrine-color">Associations</span> - Persisting associations</h3>
<pre><code contenteditable>$user = new User();
$user->setFullName('Marco Pivetta');
$objectManager->persist($user);
$address = new Address();
$address->setCity('Frankfurt')
$address->setCountry('Germany');
$objectManager->persist($address);
$project = new Project();
$project->setName('Doctrine ORM');
$objectManager->persist($project);
$user->setAddress($address);
$user->getProjects()->add($project);
$objectManager->flush();</code></pre>
</section>
<section>
<h3><span class="doctrine-color">Associations</span> - Retrieving associations</h3>
<pre><code contenteditable>$user = $objectManager->find('Application\Entity\User', 1);
var_dump($user->getAddress()->getCity()); // Frankfurt
var_dump($user->getProjects()->first()->getName()) // Doctrine ORM</code></pre>
</section>
<section>
<p>
More tutorials at
<a href="http://marco-pivetta.com/doctrine2-orm-tutorial" target="_blank" title="Doctrine 2 ORM Tutorial">
http://marco-pivetta.com/doctrine2-orm-tutorial
</a>
</p>
<p>
<img
src="assets/img/yo-dawg-presentation-in-presentation.jpg"
alt="Yo Dawg, I heard you like presentations, so I've put a presentation in your presentation"
/>
</p>
</section>
</section>
<section>
<h2><span class="zf-color">DoctrineModule</span> goodies</h2>
</section>
<section data-state="soothe">
<h2><span class="doctrine-color"><abbr title="Enhanced entity-relationship">EER</abbr> UML model</span></h2>
<p>
See what your entities look like in a graph:
</p>
<p>
<img src="assets/img/sample-eer-diagram.png" alt="Example EER UML diagram generated by DoctrineORMModule"/>
</p>
</section>
<section>
<h2><span class="zf-color">Paginator</span> Adapter</h2>
<pre><code contenteditable>use Doctrine\Common\Collections\ArrayCollection;
use DoctrineModule\Paginator\Adapter\Collection as Adapter;
use Zend\Paginator\Paginator;
// Create a Doctrine Collection
$collection = new ArrayCollection(range(1, 101));
// Create the paginator itself
$paginator = new Paginator(new Adapter($collection));
$paginator
->setCurrentPageNumber(1)
->setItemCountPerPage(5);</code></pre>
</section>
<section>
<h2>
<span class="zf-color">Paginator</span> Adapter
(<span class="doctrine-color">ORM</span>)
</h2>
<pre><code contenteditable>use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator;
use Doctrine\ORM\Tools\Pagination\Paginator as ORMPaginator;
use Zend\Paginator\Paginator;
// Create a Doctrine Collection
$query = $em->createQuery('SELECT f FROM Foo f JOIN f.bar b');
// Create the paginator itself
$paginator = new Paginator(
new DoctrinePaginator(new ORMPaginator($query))
);
$paginator
->setCurrentPageNumber(1)
->setItemCountPerPage(5);</code></pre>
</section>
<section>
<h2><span class="doctrine-color">Object-Exists</span> <span class="zf-color">Validator</span></h2>
<pre><code contenteditable>$repository = $objectManager
->getRepository('Application\Entity\User');
$validator = new \DoctrineModule\Validator\ObjectExists(array(
'object_repository' => $repository,
'fields' => array('email')
));
var_dump($validator->isValid('test@example.com'));
var_dump($validator->isValid(array(
'email' => 'test@example.com'
)));</code></pre>
</section>
<section>
<h2><span class="zf-color">Cache</span> adapters</h2>
<pre><code contenteditable>$zendCache = new \Zend\Cache\Storage\Adapter\Memory();
$cache = new \DoctrineModule\Cache\ZendStorageCache($zendCache);</code></pre>
<pre><code contenteditable>$doctrineCache = new \Doctrine\Common\Cache\ArrayCache();
$options = new \Zend\Cache\Storage\Adapter\AdapterOptions();
$cache = new \DoctrineModule\Cache\DoctrineCacheStorage(
$options,
$doctrineCache
);</code></pre>
</section>
<section>
<h2><span class="zf-color">Hydrator</span></h2>
<pre><code contenteditable>use DoctrineModule\Stdlib\Hydrator\DoctrineObject;
$hydrator = new DoctrineObject(
$objectManager,
'Application\Entity\City'
);
$city = new City();
$data = array('name' => 'Frankfurt');
$city = $hydrator->hydrate($data, $city);
echo $city->getName(); // prints "Frankfurt"
$dataArray = $hydrator->extract($city);
echo $dataArray['name']; // prints "Frankfurt"</code></pre>
</section>
<section>
<h2><span class="zf-color">Hydrator</span> (2)</h2>
<pre><code contenteditable>use DoctrineModule\Stdlib\Hydrator\DoctrineObject;
$hydrator = new DoctrineObject(
$objectManager,
'Application\Entity\City'
);
$city = new City();
$data = array('country' => 123);
$city = $hydrator->hydrate($data, $city);
var_dump($city->getCountry());
// prints class Country#1 (1) {
// protected $name => string(5) "Germany"
// }</code></pre>
</section>
<section>
<h2><span class="zf-color">Form Element</span></h2>
<pre><code contenteditable>$form->add(array(
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'name' => 'user',
'options' => array(
'object_manager' => $objectManager,
'target_class' => 'Module\Entity\User',
'property' => 'fullName',
'is_method' => true,
'find_method' => array(
'name' => 'findBy',
'params' => array(
'criteria' => array('active' => 1),
'orderBy' => array('lastName' => 'ASC'),
),
),
),
));</code></pre>
</section>
<section>
<h2>More stuff!</h2>
<p class="fragment">Everything works with <span class="doctrine-color">MongoDB ODM</span> too!</p>
<p class="fragment"><span class="doctrine-color">CouchDB ODM</span>/<span class="doctrine-color">PHPCR ODM</span>/<span class="doctrine-color">OrientDB ODM</span></p>
</section>
<section>
<section>
<h2><span class="zf-color">Good Practices</span></h2>
<img width="178" height="238" src="assets/img/down-arrow.png" alt="Down arrow"/>
</section>
<section>
<h2>Keep <span class="doctrine-color">Entities</span> simple</h2>
<p class="fragment">Think of entities as value-objects</p>
<p class="fragment">Don't add logic to entities (hard to change later!)</p>
<p class="fragment">Keep entities aware only of themselves + relations</p>
</section>
<section>
<h2>Use <span class="doctrine-color">doctrine/common</span> API</h2>
<p>
If you stick with using only <span class="doctrine-color">doctrine/common</span> API,
users of your modules can switch between <span class="doctrine-color">ORM</span>/
<span class="doctrine-color">MongoDB ODM</span>/<span class="doctrine-color">CouchDB ODM</span>/
<span class="doctrine-color">PHPCR ODM</span>/<span class="doctrine-color">OrientDB ODM</span>
</p>
</section>
<section>
<h2>Use <span class="doctrine-color">doctrine/common</span> API</h2>
<p>Prefer</p>
<pre><code contenteditable>Doctrine\Common\Persistence\ObjectManager</code></pre>
<p>over</p>
<pre><code contenteditable>Doctrine\ORM\EntityManager</code></pre>
</section>
<section>
<h2>Use <span class="doctrine-color">doctrine/common</span> API</h2>
<p>Prefer</p>
<pre><code contenteditable>Doctrine\Common\Persistence\ObjectRepository</code></pre>
<p>over</p>
<pre><code contenteditable>Doctrine\ORM\EntityRepository</code></pre>
</section>
<section>
<h2>Use <span class="doctrine-color">collections</span> extensively</h2>
<p class="fragment">
Doctrine comes with a powerful <span class="doctrine-color">collections</span>
<a href="https://github.com/doctrine/collections" target="_blank">API</a>
</p>
<p class="fragment">
OOP API for array-like data structures
</p>
</section>
<section>
<h2>Use the <span class="doctrine-color">Criteria API</span></h2>
<p class="fragment">
Collections provide a <span class="doctrine-color">Criteria</span>
<a href="https://github.com/doctrine/collections/blob/v1.0/lib/Doctrine/Common/Collections/Criteria.php" target="_blank">API</a>
</p>
<p class="fragment">
Allows you to filter virtually any kind of data structure
</p>
</section>
<section>
<h2><span class="doctrine-color">Criteria API</span> example</h2>
<pre><code contenteditable>use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\ArrayCollection;
$collection = new ArrayCollection(array($user1, $user2, $user3));
$criteria = new Criteria();
$criteria->andWhere(
$criteria->expr()->gt(
'lastLogin',
new \DateTime('-1 day')
)
);
$recentVisitors = $collection->matching($criteria);</code></pre>
<pre><code contenteditable>$recentVisitors = $em
->getRepository('Application\Entity\Users')
->matching($criteria);</code></pre>
</section>
<section>
<h2><span class="doctrine-color">Criteria API</span> advantages</h2>
<p class="fragment">
Works in <span class="doctrine-color">ORM Repositories</span>,
<span class="doctrine-color">Collections</span>, etc...
</p>
<p class="fragment">
Abstracts the problem of "searching"
</p>
<p class="fragment">
Same criteria for different storages (ORM, ODM, Memory, ElasticSearch, cache...)
</p>
<p class="fragment">
Allows you to define your own <code>RecentUsersCriteria</code> or <code>InactiveUsersCriteria</code>...
</p>
</section>
<section>
<h2>Inject the <span class="doctrine-color">Object Manager</span></h2>
<p>
If you fetch the entity manager from within your services, replacing it will become very hard:
Inject it instead!
</p>
</section>
<section>
<h2>Inject the <span class="doctrine-color">Object Manager</span></h2>
<pre><code contenteditable>'factories' => array(
'my_service' => function ($sl) {
$objectManager = $sl->get('Doctrine\ORM\EntityManager');
return new MyService($objectManager);
}
),</code></pre>
<pre><code contenteditable>class MyService
{
public function __construct(ObjectManager $objectManager)
{
// [...]
}
}</code></pre>
</section>
<section>
<h2>Keep <span class="doctrine-color">Object Manager</span> out of <span class="zf-color">Controllers</span></h2>
<p>
<img
src="assets/img/if-you-inject-objectmanager-in-your-controllers.jpg"
alt="If you inject ObjectManager in your controllers, you're gonna have a bad architecture"
/>
</p>
</section>
<section>
<h2>Don't use <span class="doctrine-color">persistence</span> to solve <span class="zf-color">application</span> problems</h2>
<p class="fragment">Filtering data when saved to DB</p>
<p class="fragment">Validating data when saved to DB</p>
<p class="fragment">Saving files when records are saved to DB</p>
<p class="fragment">Using DB-level errors to check input validity</p>
</section>
<section>
<h2>Keep your <span class="doctrine-color">object graph</span> consistent</h2>
<p class="fragment">
An <span class="doctrine-color">ObjectManager</span> works under the assumption that managed
objects are valid!
</p>
<p class="fragment">
Assign values to your entities only when data is valid!
</p>
</section>
</section>
<section>
<h2>Questions?</h2>
</section>
<section>
<h2>Fork it!</h2>
<p>
You can find these slides on GitHub at
<a href="https://github.com/Ocramius/doctrine-orm-zf2-tutorial" target="_blank">
https://github.com/Ocramius/doctrine-orm-zf2-tutorial
</a>
</p>
</section>
<section>
<h2>Thanks for watching!</h2>
</section>
</div>
</div>
<script src="reveal.js/lib/js/head.min.js"></script>
<script src="reveal.js/js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
//transition: Reveal.getQueryHash().transition || 'default', // default/cube/page/concave/zoom/linear/none
transition: Reveal.getQueryHash().transition || 'page', // default/cube/page/concave/zoom/linear/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'reveal.js/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'reveal.js/plugin/markdown/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal.js/plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal.js/plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'reveal.js/plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'reveal.js/plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
// { src: 'reveal.js/plugin/remotes/remotes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-15310667-1']);
_gaq.push(['_setDomainName', 'marco-pivetta.com']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>