Search code examples
doctrine-odmsymfony-cmf

How to create a custom PHPCRBundle initializer in doctrine/phpcr-bundle "dev-master": "1.1-dev"


I`m following the tutorial Creating a Basic CMS and i got stuck when triying to create a custom initializer for my site document class, the documentation in the symfonycmf cook book page is using doctrine/phpcr-bundle 1.0 and i'm using dev-master 1.1-dev, the reason i'm using dev-master version is because with that configuration my dependencies work fine


Solution

  • After carefully checking the two versions i realize that after all the differences between the two versions where not so big, in the 1.0 version the Doctrine\Bundle\PHPCRBundle\Initializer\InitializerInterface is injected with an PCR\SessionInterface object in his init method, that session object its like the entity manager for doctrine orm i think, as it performs the save method to presist in the odm, but in the current master version the interface inject a Doctrine\Bundle\PHPCRBundle\ManagerRegistry and with this object you can get the connection session with the method $session = $registry->getConnection(); here is the code example.

    class SiteInitializer implements InitializerInterface {
    
    
        public function init(ManagerRegistry $registry) {
    
              $session = $registry->getConnection();
              NodeHelper::createPath($session, '/cms/pages');
              NodeHelper::createPath($session, '/cms/posts');
              NodeHelper::createPath($session, '/cms/routes');
              $session->save();
              $cms = $session->getNode('/cms');
              $cms->setProperty(
                  'phpcr:class',  'Acme\BasicCmsBundle\Document\Site'
              );
    
              $session->save();
    
        }
    }
    

    @dbu thanks to you and your tean for this great work in the symfony cmf, i hope you can check this question and if you find any errors or mistakes please correct us if we are doing something wrong.