Skip to content

Commit

Permalink
Merge pull request #72 from tetranz/restore-custom-entity-manager
Browse files Browse the repository at this point in the history
Restore custom entity manager
  • Loading branch information
tetranz committed Mar 24, 2017
2 parents c0f53d5 + b1b3578 commit 0caa0aa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
20 changes: 17 additions & 3 deletions Form/Type/Select2EntityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ public function __construct(ObjectManager $em, RouterInterface $router, $config)

public function buildForm(FormBuilderInterface $builder, array $options)
{
/* @var $em ObjectManager */
$em = null;

// custom object manager for this entity, override the default entity manager ?
if(isset($options['object_manager'])) {
$em = $options['object_manager'];
if(!$em instanceof ObjectManager) {
throw new \Exception('The entity manager \'em\' must be an ObjectManager instance');
}
} else {
// else, we use the default entity manager
$em = $this->em;
}

// add custom data transformer
if ($options['transformer']) {
if (!is_string($options['transformer'])) {
Expand All @@ -51,7 +65,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
throw new \Exception('Unable to load class: '.$options['transformer']);
}

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

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

$transformer = $options['multiple']
? new EntitiesToPropertyTransformer($this->em, $options['class'], $options['text_property'], $options['primary_key'], $newTagPrefix)
: new EntityToPropertyTransformer($this->em, $options['class'], $options['text_property'], $options['primary_key'], $newTagPrefix);
? new EntitiesToPropertyTransformer($em, $options['class'], $options['text_property'], $options['primary_key'], $newTagPrefix)
: new EntityToPropertyTransformer($em, $options['class'], $options['text_property'], $options['primary_key'], $newTagPrefix);
}

$builder->addViewTransformer($transformer, true);
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ $builder
'cache_timeout' => 60000, // if 'cache' is true
'language' => 'en',
'placeholder' => 'Select a country',
// 'em' => $entityManager, // inject a custom entity manager
// 'object_manager' => $objectManager, // inject a custom object / entity manager
])
```
Put this at the top of the file with the form type class:
Expand Down

0 comments on commit 0caa0aa

Please sign in to comment.