Example #1
0
    /**
     * Init doctrine method
     *
     * @return Shopware\Components\Model\ModelManager
     */
    public function initModels()
    {
       /** @var $config \Doctrine\ORM\Configuration */
        $config = $this->getResource('ModelConfig');

        $cacheResource = $this->getResource('Cache');

        // Check if native Doctrine ApcCache may be used
        if ($cacheResource->getBackend() instanceof Zend_Cache_Backend_Apc) {
            $cache = new Doctrine\Common\Cache\ApcCache();
        } else {
            $cache = new Shopware\Components\Model\Cache($cacheResource);
        }

        // register standard doctrine annotations
        Doctrine\Common\Annotations\AnnotationRegistry::registerFile(
            'Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'
        );

        // register symfony validation annotions
        Doctrine\Common\Annotations\AnnotationRegistry::registerAutoloadNamespace(
            'Symfony\Component\Validator\Constraint'
        );

        // register gedmo annotions
        Doctrine\Common\Annotations\AnnotationRegistry::registerFile(
            'Gedmo/Mapping/Annotation/All.php'
        );

        $cachedAnnotationReader = new Doctrine\Common\Annotations\CachedReader(
            new Doctrine\Common\Annotations\AnnotationReader,
            $cache
        );

        $annotationDriver = new Doctrine\ORM\Mapping\Driver\AnnotationDriver(
            $cachedAnnotationReader, array(
            $this->Application()->Loader()->isReadable('Gedmo/Tree/Entity/MappedSuperclass'),
            $this->Application()->AppPath('Models')
        ));

        // create a driver chain for metadata reading
        $driverChain = new Doctrine\ORM\Mapping\Driver\DriverChain();

        // register annotation driver for our application
        $driverChain->addDriver($annotationDriver, 'Gedmo');
        $driverChain->addDriver($annotationDriver, 'Shopware\\Models\\');
        $driverChain->addDriver($annotationDriver, 'Shopware\\CustomModels\\');

        $this->registerResource('ModelAnnotations', $annotationDriver);

        $config->setMetadataDriverImpl($driverChain);

        // Create event Manager
        $eventManager = new \Doctrine\Common\EventManager();

        $treeListener = new Gedmo\Tree\TreeListener;
        $treeListener->setAnnotationReader($cachedAnnotationReader);
        $eventManager->addEventSubscriber($treeListener);

        // Create new shopware event subscriber to handle the entity lifecycle events.
        $liveCycleSubscriber = new \Shopware\Components\Model\EventSubscriber(
            $this->Application()->Events()
        );
        $eventManager->addEventSubscriber($liveCycleSubscriber);

        // now create the entity manager and use the connection
        // settings we defined in our application.ini
        $conn = \Doctrine\DBAL\DriverManager::getConnection(
            array('pdo' => $this->Application()->Db()->getConnection()),
            $config,
            $eventManager
        );

        $entityManager = Shopware\Components\Model\ModelManager::create($conn, $config, $eventManager);

        //if (!is_readable(rtrim($config->getProxyDir(), '/') . '/__CG__ShopwareModelsShopShop.php')) {
        //    $metadata     = $entityManager->getMetadataFactory()->getAllMetadata();
        //    $proxyFactory = $entityManager->getProxyFactory();
        //    $proxyFactory->generateProxyClasses($metadata);
        //}

        return $entityManager;
    }