Reputation: 479
I'm creating a bundle with relationship associations. In order to keep things abstract, I'd like to use Doctrine's brand new ResolveTargetEntities
listener.
The thing is that I would like the setup of the listener to be automated, so future developers using my bundle won't need to configure the listener themselves.
In my bundle, there's a config parameter called data_class
, which I would like to use to setup the ResolveTargetEntities
listener:
# app/config/config.yml
my_bundle:
City:
data_class: Acme\DemoBundle\Entity\City
How can I setup a service, or a config file within my bundle to configure the listener using this parameter? Something like this:
resolve_target_entities:
Dev\MyBundle\Model\City: %my_bundle.City.data_class%
EDIT:
The above configuration example is provided to show what should be accomplished by doctrine, but the object of this question is to find a way to setup the ResolveTargetEntities
listener automatically, using a service, a dependency injection container, or any other way that requires the end user to provide only one parameter under the my_bundle
namespace: data_class
Upvotes: 2
Views: 2813
Reputation: 444
You can use your Bundle Extension class in DependancyInjection folder for that. There is a option to add custom configuration to another bundles configuration (http://symfony.com/doc/current/cookbook/bundles/prepend_extension.html). My code that does the same is below.
<?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;
}
}
}
}
}
Upvotes: 4
Reputation: 6065
Next, we need to configure the listener. Add this to the area you set up Doctrine. You must set this up in the way outlined below, otherwise you can not be guaranteed that the targetEntity resolution will occur reliably: $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);
Upvotes: 1
Reputation: 1
Try writing
my_bundle.City.data_class: Acme\DemoBundle\Entity\City
in the parameters.yml file, it worked to me.
Upvotes: 0