Skip to content

Commit 0caa0aa

Browse files
authored
Merge pull request #72 from tetranz/restore-custom-entity-manager
Restore custom entity manager
2 parents c0f53d5 + b1b3578 commit 0caa0aa

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

Form/Type/Select2EntityType.php

+17-3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ public function __construct(ObjectManager $em, RouterInterface $router, $config)
4242

4343
public function buildForm(FormBuilderInterface $builder, array $options)
4444
{
45+
/* @var $em ObjectManager */
46+
$em = null;
47+
48+
// custom object manager for this entity, override the default entity manager ?
49+
if(isset($options['object_manager'])) {
50+
$em = $options['object_manager'];
51+
if(!$em instanceof ObjectManager) {
52+
throw new \Exception('The entity manager \'em\' must be an ObjectManager instance');
53+
}
54+
} else {
55+
// else, we use the default entity manager
56+
$em = $this->em;
57+
}
58+
4559
// add custom data transformer
4660
if ($options['transformer']) {
4761
if (!is_string($options['transformer'])) {
@@ -51,7 +65,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
5165
throw new \Exception('Unable to load class: '.$options['transformer']);
5266
}
5367

54-
$transformer = new $options['transformer']($this->em, $options['class'], $options['text_property'], $options['primary_key']);
68+
$transformer = new $options['transformer']($em, $options['class'], $options['text_property'], $options['primary_key']);
5569

5670
if (!$transformer instanceof DataTransformerInterface) {
5771
throw new \Exception(sprintf('The custom transformer %s must implement "Symfony\Component\Form\DataTransformerInterface"', get_class($transformer)));
@@ -67,8 +81,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
6781
}
6882

6983
$transformer = $options['multiple']
70-
? new EntitiesToPropertyTransformer($this->em, $options['class'], $options['text_property'], $options['primary_key'], $newTagPrefix)
71-
: new EntityToPropertyTransformer($this->em, $options['class'], $options['text_property'], $options['primary_key'], $newTagPrefix);
84+
? new EntitiesToPropertyTransformer($em, $options['class'], $options['text_property'], $options['primary_key'], $newTagPrefix)
85+
: new EntityToPropertyTransformer($em, $options['class'], $options['text_property'], $options['primary_key'], $newTagPrefix);
7286
}
7387

7488
$builder->addViewTransformer($transformer, true);

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ $builder
9595
'cache_timeout' => 60000, // if 'cache' is true
9696
'language' => 'en',
9797
'placeholder' => 'Select a country',
98-
// 'em' => $entityManager, // inject a custom entity manager
98+
// 'object_manager' => $objectManager, // inject a custom object / entity manager
9999
])
100100
```
101101
Put this at the top of the file with the form type class:

0 commit comments

Comments
 (0)