Slide 1

Slide 1 text

What is new in Doctrine? Benjamin Eberlei & Alexander Mols Symfony Live Berlin 2012

Slide 2

Slide 2 text

Benjamin Benjamin Eberlei SimpleThings GmbH Doctrine Core Developer github.com/beberlei twitter.com/beberlei

Slide 3

Slide 3 text

Alexander Alexander Mols Technical Lead Developer @ VONQ Doctrine Core Developer github.com/asm89 twitter.com/iam_asm89

Slide 4

Slide 4 text

The big picture ● 6 month release schedule ● 2.4 on the horizon ● Increased velocity since 2.0 ● Growing team ● Stabilizing and improving minor details

Slide 5

Slide 5 text

New features ● 2.2 and 2.3 added tons of features ● Hard to keep up with rate of change ● Documentation fail on our part

Slide 6

Slide 6 text

Filter API http://www.flickr.com/photos/giantsqurl/4983106796/

Slide 7

Slide 7 text

Filter API 1 reflClass->getInterfaceNames())) { 10 return ""; 11 } 12 13 return $targetTableAlias . '.locale = ' . $this->getParameter('locale'); 14 } 15 }

Slide 8

Slide 8 text

Filter API 1 getFilters()->enable('locale'); 4 $filter->setParameter('locale', 'en'); 5 6 // disable a filter 7 $filter = $entityManager->getFilters()->disable('softdelete');

Slide 9

Slide 9 text

Filter API 1 # entity manager configuration 2 filters: 3 locale: 4 class: Acme\MyLocaleFilter 5 softdelete: 6 class: Acme\MySoftdeleteFilter 7 enabled: true

Slide 10

Slide 10 text

Complex SQL Types http://www.flickr.com/photos/garrettc/2172850095/

Slide 11

Slide 11 text

Complex SQL Types 1

Slide 12

Slide 12 text

Flush Optimizations http://www.flickr.com/photos/elycefeliz/5717273714/

Slide 13

Slide 13 text

1 find("User", 1); 3 $group = $user->getGroup(); 4 5 // do something with $user and $group 6 7 // single flush 8 $entityManager->flush($user); 9 10 // set flush 11 $entityManager->flush(array($user, $group));

Slide 14

Slide 14 text

Paginator http://www.flickr.com/photos/horiavarlan/4332381194/

Slide 15

Slide 15 text

Paginator ● Merger of three libraries: – Doctrine extensions – Pagerfanta – KnpLabs pager ● Used in various bundles now

Slide 16

Slide 16 text

1 createQuery($dql) 6 ->setFirstResult(0) 7 ->setMaxResults(100); 8 9 $paginator = new Paginator($query); 10 11 echo 'Found ' . count($paginator) . ' posts!' . "\n"; 12 foreach ($paginator as $post) { 13 echo $post->getHeadline() . "\n"; 14 }

Slide 17

Slide 17 text

DQL Improvements

Slide 18

Slide 18 text

DQL Improvements 1 createQuery($dql)->getResult(); 11 12 foreach ($results as $row) { 13 $post = $row['post']; 14 $count = $row['comment_count']; 15 }

Slide 19

Slide 19 text

DQL Improvements 1

Slide 20

Slide 20 text

DQL Improvements ● Bit comparisons ● New CASE expression ● New IDENTITY function ● Sort by associations

Slide 21

Slide 21 text

Master-Slave Connection

Slide 22

Slide 22 text

Master-Slave Connection ● Define many slaves and one master ● All reads go to a random slave ● Until one write statement is issued ● Then every read goes to master ● Selectively change back to slave

Slide 23

Slide 23 text

Target Entity Resolving

Slide 24

Slide 24 text

Target Entity Resolving 1

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

Arbitrary Joins

Slide 27

Slide 27 text

Arbitrary Joins 1 SELECT 2 u.name, a.title 3 FROM 4 User u 5 JOIN 6 Article a 7 WITH 8 a.user = u.id 1 SELECT 2 u.name, a.title 3 FROM 4 User u 5 JOIN 6 u.article a

Slide 28

Slide 28 text

Arbitrary Joins 1 SELECT 2 u.name, a.title 3 FROM 4 User u 5 JOIN 6 Article a 7 WITH 8 a.user = u.id 1 SELECT 2 u.name, a.title 3 FROM 4 User u 5 JOIN 6 u.article a

Slide 29

Slide 29 text

Collection Criteria

Slide 30

Slide 30 text

Collection Criteria Filter associations without DQL: 1 getUsers(); 5 6 $criteria = Criteria::create() 7 ->where(Criteria::expr()->eq("birthday", "11-23")) 8 ->orderBy(array("username" => "ASC")) 9 ->setFirstResult(0) 10 ->setMaxResults(20) 11 ; 12 13 $birthdayUsers = $userCollection->matching($criteria);

Slide 31

Slide 31 text

Collection Criteria Filter associations without DQL: 1 getUsers(); 5 6 $criteria = Criteria::create() 7 ->where(Criteria::expr()->eq("birthday", "11-23")) 8 ->orderBy(array("username" => "ASC")) 9 ->setFirstResult(0) 10 ->setMaxResults(20) 11 ; 12 13 $birthdayUsers = $userCollection->matching($criteria);

Slide 32

Slide 32 text

Collection Criteria Filter associations without DQL: 1 getUsers(); 5 6 $criteria = Criteria::create() 7 ->where(Criteria::expr()->eq("birthday", "11-23")) 8 ->orderBy(array("username" => "ASC")) 9 ->setFirstResult(0) 10 ->setMaxResults(20) 11 ; 12 13 $birthdayUsers = $userCollection->matching($criteria);

Slide 33

Slide 33 text

Collection Criteria Filter associations without DQL: 1 getUsers(); 5 6 $criteria = Criteria::create() 7 ->where(Criteria::expr()->eq("birthday", "11-23")) 8 ->orderBy(array("username" => "ASC")) 9 ->setFirstResult(0) 10 ->setMaxResults(20) 11 ; 12 13 $birthdayUsers = $userCollection->matching($criteria);

Slide 34

Slide 34 text

Collection Criteria Filter associations without DQL: 1 getUsers(); 5 6 $criteria = Criteria::create() 7 ->where(Criteria::expr()->eq("birthday", "11-23")) 8 ->orderBy(array("username" => "ASC")) 9 ->setFirstResult(0) 10 ->setMaxResults(20) 11 ; 12 13 $birthdayUsers = $userCollection->matching($criteria);

Slide 35

Slide 35 text

Collection Criteria Filter associations without DQL: 1 getUsers(); 5 6 $criteria = Criteria::create() 7 ->where(Criteria::expr()->eq("birthday", "11-23")) 8 ->orderBy(array("username" => "ASC")) 9 ->setFirstResult(0) 10 ->setMaxResults(20) 11 ; 12 13 $birthdayUsers = $userCollection->matching($criteria);

Slide 36

Slide 36 text

Collection Criteria ● Uses SQL when collection is not yet loaded ● Works on any Selectable – PersistentCollection (associations) – ArrayCollection – EntityReposity

Slide 37

Slide 37 text

Naming Strategy

Slide 38

Slide 38 text

Naming Strategy ● Customize name generation for tables, columns ● Two strategies shipped in core: – DefaultNamingStrategy – UnderscoreNamingStrategy ● Only works for “unnamed” columns! ● Legacy database schema's

Slide 39

Slide 39 text

Naming Strategy 1 setNamingStrategy($namingStrategy);

Slide 40

Slide 40 text

Custom ID Generators http://www.flickr.com/photos/mheinzel/5492534374/

Slide 41

Slide 41 text

Custom ID Generators 1

Slide 42

Slide 42 text

Custom ID Generators 1

Slide 43

Slide 43 text

DoctrineBundle

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

DoctrineBundle

Slide 46

Slide 46 text

Also in 2.3

Slide 47

Slide 47 text

Also in 2.3 ● Possibility to override metadata in subclasses: – Associations – Properties ● @NamedNativeQuery ● @SqlResultSetMapping(s)

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

NEW() Operator

Slide 50

Slide 50 text

NEW() Operator 1

Slide 51

Slide 51 text

ResultSetMapping Builder

Slide 52

Slide 52 text

ResultSetMapping Builder 1 getRepository('User') 4 ->createResultSetMappingBuilder('u'); 5 $rsm->addJoinedEntityFromClassMetadata('Group', 'g'); 6 7 $sql = <<createNativeQuery($sql, $rsm) 13 ->getResult();

Slide 53

Slide 53 text

EventListeners

Slide 54

Slide 54 text

EventListeners 1

Slide 55

Slide 55 text

EventListeners 1

Slide 56

Slide 56 text

Questions? Feedback @ http://joind.in/talk/view/7567 Benjamin Eberlei Alexander Mols