Symfony 2.2 Doctrine ResolveTargetEntities Listener

时间:2013-01-20 13:10:14

标签: symfony doctrine entity-relationship symfony-2.2

我正在创建一个包含关系关联的包。为了保持抽象,我想使用Doctrine全新的ResolveTargetEntities听众。

问题是我希望监听器的设置是自动化的,因此使用我的bundle的未来开发人员不需要自己配置监听器。

在我的捆绑包中,有一个名为data_class的配置参数,我想用它来设置ResolveTargetEntities监听器:

# app/config/config.yml
my_bundle:
    City:
        data_class: Acme\DemoBundle\Entity\City

如何在我的包中设置服务或配置文件以使用此参数配置侦听器?像这样:

resolve_target_entities:
    Dev\MyBundle\Model\City: %my_bundle.City.data_class%

编辑:

上面的配置示例是为了显示doctrine应该完成的内容,但是这个问题的目的是找到一种方法来自动设置ResolveTargetEntities侦听器,使用服务,依赖注入容器,或者要求最终用户仅在my_bundle命名空间下提供一个参数的任何其他方式:data_class

3 个答案:

答案 0 :(得分:4)

您可以在DependancyInjection文件夹中使用Bundle Extension类。可以选择将自定义配置添加到另一个捆绑包配置(http://symfony.com/doc/current/cookbook/bundles/prepend_extension.html)。我的代码也是如此。

<?php

namespace Zveen\CmsBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

/**
 * This is the class that loads and manages your bundle configuration
 *
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
 */
class ZveenCmsExtension extends Extension implements PrependExtensionInterface
{
    /**
     * {@inheritDoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $container->setParameter('zveen.cmsbundle.tenant.class', $config['tenant']['class']);
        $container->setParameter('zveen.cmsbundle.tenant.dql', $config['tenant']['class']);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
    }

    /**
     * Allow an extension to prepend the extension configurations.
     *
     * @param ContainerBuilder $container
     */
    public function prepend(ContainerBuilder $container)
    {
        // get all Bundles
        $bundles = $container->getParameter('kernel.bundles');
        if (isset($bundles['DoctrineBundle'])) {
            // Get configuration of our own bundle
            $configs = $container->getExtensionConfig($this->getAlias());
            $config = $this->processConfiguration(new Configuration(), $configs);

            // Prepare for insertion
            $forInsertion = array(
                'orm' => array(
                    'resolve_target_entities' => array(
                        'Zveen\CmsBundle\Entity\TenantInterface' => $config['tenant']['class']
                    )
                )
            );
            foreach ($container->getExtensions() as $name => $extension) {
                switch ($name) {
                    case 'doctrine':
                        $container->prependExtensionConfig($name, $forInsertion);
                        break;
                }
            }
        }
    }
}

答案 1 :(得分:1)

请参阅https://github.com/doctrine/doctrine2/blob/master/docs/en/cookbook/resolve-target-entity-listener.rst

  

接下来,我们需要配置监听器。将其添加到您设置Doctrine的区域。您必须按照下面概述的方式进行设置,否则无法保证targetEntity解析将可靠地发生:       $ evm = new \ Doctrine \ Common \ EventManager;

$rtel = new \Doctrine\ORM\Tools\ResolveTargetEntityListener;
$rtel->addResolveTargetEntity('Acme\\InvoiceModule\\Model\\InvoiceSubjectInterface',
    'Acme\\CustomerModule\\Entity\\Customer', array());

// Add the ResolveTargetEntityListener
$evm->addEventSubscriber($rtel);

$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config, $evm);

答案 2 :(得分:0)

尝试写作

my_bundle.City.data_class: Acme\DemoBundle\Entity\City

在parameters.yml文件中,它对我有用。