-
-
Save sound-code/f1fad6dbce7c05abcd79 to your computer and use it in GitHub Desktop.
modifica entity manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace PUGX\AutocompleterBundle\Form\Type; | |
use Doctrine\Common\Persistence\ManagerRegistry; | |
use Doctrine\Common\Persistence\ObjectManager; | |
use PUGX\AutocompleterBundle\Form\Transformer\ObjectToIdTransformer; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\Exception\InvalidConfigurationException; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
class AutocompleteType extends AbstractType | |
{ | |
private $managerRegistry; | |
/** | |
* @param ManagerRegistry $managerRegistry | |
*/ | |
public function __construct(ManagerRegistry $managerRegistry) | |
{ | |
$this->managerRegistry = $managerRegistry; | |
} | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
if (empty($options['class'])) { | |
throw new InvalidConfigurationException('Option "class" must be set.'); | |
} | |
$entityManager = $this->managerRegistry->getManagerForClass($options['class']); | |
$transformer = new ObjectToIdTransformer($entityManager, $options['class']); | |
$builder->addModelTransformer($transformer); | |
} | |
public function setDefaultOptions(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'class' => '', | |
'invalid_message' => 'The selected item does not exist', | |
)); | |
} | |
public function getParent() | |
{ | |
return 'text'; | |
} | |
public function getName() | |
{ | |
return 'autocomplete'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment