X-Git-Url: http://git.inspyration.org/?a=blobdiff_plain;f=vendor%2Fdoctrine%2Form%2Ftests%2FDoctrine%2FTests%2FORM%2FFunctional%2FFlushEventTest.php;fp=vendor%2Fdoctrine%2Form%2Ftests%2FDoctrine%2FTests%2FORM%2FFunctional%2FFlushEventTest.php;h=8167e85876605ccbaae357295c8d449f1fa96a2e;hb=8b04b2d11798dee4f3e1358e4f43e97a6df851f6;hp=0000000000000000000000000000000000000000;hpb=73568cf05a785a45f94ca3f2351d9e07bf917958;p=zf2.biz%2Fapplication_blanche.git diff --git a/vendor/doctrine/orm/tests/Doctrine/Tests/ORM/Functional/FlushEventTest.php b/vendor/doctrine/orm/tests/Doctrine/Tests/ORM/Functional/FlushEventTest.php new file mode 100644 index 0000000..8167e85 --- /dev/null +++ b/vendor/doctrine/orm/tests/Doctrine/Tests/ORM/Functional/FlushEventTest.php @@ -0,0 +1,94 @@ +useModelSet('cms'); + parent::setUp(); + } + + public function testPersistNewEntitiesOnPreFlush() + { + //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); + $this->_em->getEventManager()->addEventListener(Events::onFlush, new OnFlushListener); + + $user = new CmsUser; + $user->username = 'romanb'; + $user->name = 'Roman'; + $user->status = 'Dev'; + + $this->_em->persist($user); + + $this->assertEquals(0, $user->phonenumbers->count()); + + $this->_em->flush(); + + $this->assertEquals(1, $user->phonenumbers->count()); + $this->assertTrue($this->_em->contains($user->phonenumbers->get(0))); + $this->assertTrue($user->phonenumbers->get(0)->getUser() === $user); + + $this->assertFalse($user->phonenumbers->isDirty()); + + // Can be used together with SQL Logging to check that a subsequent flush has + // nothing to do. This proofs the correctness of the changes that happened in onFlush. + //echo "SECOND FLUSH"; + //$this->_em->flush(); + } +} + +class OnFlushListener +{ + public function onFlush(OnFlushEventArgs $args) + { + //echo "---preFlush".PHP_EOL; + + $em = $args->getEntityManager(); + $uow = $em->getUnitOfWork(); + + foreach ($uow->getScheduledEntityInsertions() as $entity) { + + if ($entity instanceof CmsUser) { + // Adds a phonenumber to every newly persisted CmsUser ... + + $phone = new CmsPhonenumber; + $phone->phonenumber = 12345; + // Update object model + $entity->addPhonenumber($phone); + // Invoke regular persist call + $em->persist($phone); + // Explicitly calculate the changeset since onFlush is raised + // after changeset calculation! + $uow->computeChangeSet($em->getClassMetadata(get_class($phone)), $phone); + + // Take a snapshot because the UoW wont do this for us, because + // the UoW did not visit this collection. + // Alternatively we could provide an ->addVisitedCollection() method + // on the UoW. + $entity->getPhonenumbers()->takeSnapshot(); + } + + /*foreach ($uow->getEntityChangeSet($entity) as $field => $change) { + list ($old, $new) = $change; + + var_dump($old); + }*/ + + } + } +} + +