1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
-
- namespace Doctrine\Tests\ORM\Functional;
-
- use Doctrine\Tests\Models\CMS\CmsUser;
- use Doctrine\Tests\Models\CMS\CmsPhonenumber;
- use Doctrine\ORM\Event\OnFlushEventArgs;
- use Doctrine\ORM\Events;
-
- require_once __DIR__ . '/../../TestInit.php';
-
-
- class FlushEventTest extends \Doctrine\Tests\OrmFunctionalTestCase
- {
- protected function setUp() {
- $this->useModelSet('cms');
- parent::setUp();
- }
-
- public function testPersistNewEntitiesOnPreFlush()
- {
-
- $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());
-
-
-
-
-
- }
- }
-
- class OnFlushListener
- {
- public function onFlush(OnFlushEventArgs $args)
- {
-
-
- $em = $args->getEntityManager();
- $uow = $em->getUnitOfWork();
-
- foreach ($uow->getScheduledEntityInsertions() as $entity) {
-
- if ($entity instanceof CmsUser) {
-
-
- $phone = new CmsPhonenumber;
- $phone->phonenumber = 12345;
-
- $entity->addPhonenumber($phone);
-
- $em->persist($phone);
-
-
- $uow->computeChangeSet($em->getClassMetadata(get_class($phone)), $phone);
-
-
-
-
-
- $entity->getPhonenumbers()->takeSnapshot();
- }
-
-
-
-
- }
- }
- }
-
-
|