Networking\InitCmsBundle\Entity\PageListener::onFlush PHP Метод

onFlush() публичный Метод

public onFlush ( Doctrine\ORM\Event\OnFlushEventArgs $args ) : mixed | void
$args Doctrine\ORM\Event\OnFlushEventArgs
Результат mixed | void
    public function onFlush(OnFlushEventArgs $args)
    {
        $em = $args->getEntityManager();
        $unitOfWork = $em->getUnitOfWork();
        foreach ($unitOfWork->getScheduledEntityUpdates() as $entity) {
            if ($entity instanceof PageInterface) {
                if ($contentRoute = $entity->getContentRoute()) {
                    $contentRoute->setPath(PageHelper::getPageRoutePath($entity->getPath()));
                    $em->persist($contentRoute);
                    $unitOfWork->computeChangeSet($em->getClassMetadata(get_class($contentRoute)), $contentRoute);
                    foreach ($entity->getAllChildren() as $child) {
                        $contentRoute = $child->getContentRoute();
                        $contentRoute->setPath(PageHelper::getPageRoutePath($child->getPath()));
                        $em->persist($contentRoute);
                        $unitOfWork->computeChangeSet($em->getClassMetadata(get_class($contentRoute)), $contentRoute);
                        if ($entity->getStatus() == Page::STATUS_PUBLISHED) {
                            if ($childSnapshot = $child->getSnapshot()) {
                                $snapshotRoute = $childSnapshot->getContentRoute();
                                $newPath = PageHelper::getPageRoutePath($child->getPath());
                                $snapshotRoute->setPath($newPath);
                                $childSnapshot->setPath($newPath);
                                $em->persist($childSnapshot);
                                $em->persist($snapshotRoute);
                                $unitOfWork->computeChangeSet($em->getClassMetadata(get_class($childSnapshot)), $childSnapshot);
                                $unitOfWork->computeChangeSet($em->getClassMetadata(get_class($snapshotRoute)), $snapshotRoute);
                            }
                        }
                    }
                }
            }
        }
    }

Usage Example

Пример #1
0
 /**
  * postUpdate with Page and 10 children
  */
 public function testPostUpdate_WithPageAndTenChildren()
 {
     $container = $this->getMock('\\Symfony\\Component\\DependencyInjection\\Container');
     $pageListener = new PageListener(new \Symfony\Component\HttpFoundation\Session\Session(), $container);
     // contentRoute
     $contentRoute = $this->getMock('\\Networking\\InitCmsBundle\\Model\\ContentRoute', array('setPath'));
     $contentRoute->expects($this->once())->method('setPath')->with($this->equalTo('/'));
     // entity
     $entity = $this->getMock('\\Networking\\InitCmsBundle\\Model\\Page');
     $entity->expects($this->once())->method('getContentRoute')->will($this->returnValue($contentRoute));
     $entity->expects($this->once())->method('getAllChildren')->will($this->returnValue($this->getMockChildren(10)));
     // em
     $em = $this->getMockBuilder('\\Doctrine\\ORM\\EntityManager')->disableOriginalConstructor()->getMock();
     $uow = $this->getMockBuilder('\\Doctrine\\ORM\\UnitOfWork')->disableOriginalConstructor()->getMock();
     $em->expects($this->once())->method('getUnitOfWork')->will($this->returnValue($uow));
     $classMetaData = $this->getMockBuilder('\\Doctrine\\ORM\\Mapping\\ClassMetadata')->disableOriginalConstructor()->getMock();
     $em->expects($this->exactly(11))->method('getClassMetadata')->will($this->returnValue($classMetaData));
     $em->expects($this->exactly(11))->method('persist');
     $uow->expects($this->once())->method('getScheduledEntityUpdates')->will($this->returnValue(array($entity)));
     $uow->expects($this->exactly(11))->method('computeChangeSet');
     $args = $this->getMockBuilder('\\Doctrine\\ORM\\Event\\OnFlushEventArgs')->disableOriginalConstructor()->getMock();
     // args methods:
     // getEntityManager
     $args->expects($this->once())->method('getEntityManager')->will($this->returnValue($em));
     $pageListener->onFlush($args);
 }