Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added support for multiple entity managers in entity choice field types

  • Loading branch information...
commit 6aa0bcc833c392f1345487f8f4a464a9b08b7ba1 1 parent 721b3fc
@mgiagnoni authored
View
40 DependencyInjection/LyraAdminExtension.php
@@ -281,10 +281,13 @@ private function updateColumnsDefaults()
private function setFieldsDefaultsFromMetadata()
{
- $classes = array();
+ $classes = $managers = array();
foreach ($this->modelNames as $model) {
$options = $this->config['models'][$model];
$classes[$options['class']] = array('model' => $model, 'fields' => $options['fields']);
+ if (isset($options['entity_manager'])) {
+ $managers[$options['class']] = $options['entity_manager'];
+ }
}
foreach ($this->modelNames as $model) {
@@ -348,6 +351,10 @@ private function setFieldsDefaultsFromMetadata()
'multiple' => ClassMetadataInfo::MANY_TO_MANY == $attrs['type']
);
+ if (isset($managers[$class])) {
+ $fields[$name]['options']['em'] = $managers[$class]['name'];
+ }
+
if (isset($classes[$class])) {
$fields[$name]['assoc'] = $classes[$class];
}
@@ -399,10 +406,8 @@ private function setFilterFieldsDefaults()
break;
case 'entity':
$filters[$field]['options'] = array_merge(
- $filters[$field]['options'], array(
- 'class' => $fields[$field]['options']['class'],
- 'multiple' => $fields[$field]['options']['multiple']
- )
+ $filters[$field]['options'],
+ array_intersect_key($fields[$field]['options'], array_flip(array('class', 'multiple', 'em')))
);
break;
}
@@ -471,7 +476,7 @@ private function createActionsDefinition($model, $options, ContainerBuilder $con
private function createModelManagerDefinition($model, $options, ContainerBuilder $container)
{
$container->setDefinition(sprintf('lyra_admin.%s.model_manager', $model), new DefinitionDecorator($options['services']['model_manager']))
- ->setArguments(array(new Reference('doctrine'), new Reference(sprintf('lyra_admin.%s.configuration', $model))));
+ ->setArguments(array(new Reference('doctrine.orm.entity_manager'), new Reference(sprintf('lyra_admin.%s.configuration', $model))));
}
@@ -595,6 +600,11 @@ private function updateServiceDefinitions(ContainerBuilder $container)
{
foreach ($this->config['models'] as $model => $options) {
+ if(isset($options['entity_manager'])) {
+ $container->getDefinition(sprintf('lyra_admin.%s.model_manager', $model))
+ ->replaceArgument(0, new Reference($options['entity_manager']['id']));
+ }
+
$container->getDefinition(sprintf('lyra_admin.%s.grid_columns', $model))
->setArguments(array($options['list']['columns']));
@@ -678,9 +688,23 @@ private function setMenuOptions(ContainerBuilder $container)
private function readMetadata(ContainerBuilder $container)
{
+ if (!$container->has('doctrine')) {
+ return;
+ }
+
+ $emNames = $container->get('doctrine')->getEntityManagerNames();
+
foreach ($this->config['models'] as $model => $options) {
- if ($em = $container->get('doctrine')->getEntityManagerForClass($options['class'])) {
- $this->metadata[$model] = $em->getClassMetadata($options['class']);
+ foreach ($emNames as $name => $id) {
+ $em = $container->get($id);
+ if (!$em->getConfiguration()->getMetadataDriverImpl()->isTransient($options['class'])) {
+ $this->metadata[$model] = $em->getClassMetadata($options['class']);
+ $this->config['models'][$model]['entity_manager'] = array(
+ 'name' => $name,
+ 'id' => $id
+ );
+ break;
+ }
}
}
}
View
7 Model/ORM/ModelManager.php
@@ -14,7 +14,6 @@
use Lyra\AdminBundle\Model\ModelManager as BaseManager;
use Lyra\AdminBundle\Configuration\AdminConfigurationInterface;
use Doctrine\ORM\EntityManager;
-use Symfony\Bridge\Doctrine\RegistryInterface;
/**
* Generic model manager class (Doctrine ORM).
@@ -25,11 +24,11 @@ class ModelManager extends BaseManager
protected $class;
- public function __construct(RegistryInterface $registry, AdminConfigurationInterface $configuration)
+ public function __construct(EntityManager $em, AdminConfigurationInterface $configuration)
{
- $this->setClass($configuration->getOption('class'));
- $this->em = $registry->getEntityManagerForClass($this->getClass());
+ $this->em = $em;
$this->configuration = $configuration;
+ $this->setClass($configuration->getOption('class'));
}
public function find($id)
Please sign in to comment.
Something went wrong with that request. Please try again.