2

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

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

在我的包中,有一个名为 的配置参数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%

编辑:

上面的配置示例是为了展示学说应该完成的事情,但这个问题的目的是找到一种方法来ResolveTargetEntities自动设置监听器,使用服务、依赖注入容器或任何其他需要最终用户的方式在命名空间下只提供一个参数my_bundledata_class

4

3 回答 3

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;
                }
            }
        }
    }
}
于 2014-02-21T10:49:28.397 回答
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);
于 2013-03-14T09:43:46.353 回答
0

尝试写作

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

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

于 2013-01-21T02:27:20.160 回答