Created
March 28, 2013 14:05
-
-
Save iamdey/5263372 to your computer and use it in GitHub Desktop.
Symfony2 form widget mixed between entity & choice
This file contains hidden or 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 Acme\DemoBundle\Form\Type; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
use Doctrine\Bundle\DoctrineBundle\Registry; | |
use Symfony\Component\OptionsResolver\Options; | |
use Doctrine\Common\Util\Inflector; | |
use Symfony\Component\Form\Exception\FormException; | |
/** | |
* DoctrineChoiceType est le chainon manquant entre le type choice et le type entity | |
* | |
* permet de faire une liste de choix clé => valeur depuis une table | |
* | |
* @author David Epely <[email protected]> | |
*/ | |
class DoctrineChoiceType extends AbstractType | |
{ | |
protected $registry; | |
public function __construct(Registry $registry) | |
{ | |
$this->registry = $registry; | |
} | |
public function setDefaultOptions(OptionsResolverInterface $resolver) | |
{ | |
$registry = $this->registry; | |
//closure qui sera passé au widget parent pour définir les choix | |
$choices = function(Options $options) use ($registry) { | |
if (!isset($options['class']) || empty($options['class'])) { | |
throw new FormException('class must be defined'); | |
} | |
if (!isset($options['property']) || empty($options['property'])) { | |
throw new FormException('property must be defined'); | |
} | |
//repository de la classe à lister | |
$repository = $registry->getRepository($options['class']); | |
$property = Inflector::classify($options['property']); | |
$propertyValue = ($options['property_value']) ? $options['property_value'] : $property; | |
//résultats de la requete : tableau d'entités | |
if ($options['query_builder']) { | |
$results = $options['query_builder']($repository) | |
->getQuery() | |
->execute(); | |
} else { | |
//par défaut on liste tout | |
$results = $repository->findAll(); | |
} | |
//géneration d'un tableau simple clé => valeur fournit au widget parent : choice | |
$choices = array(); | |
array_map(function($entity) use ($property, $propertyValue, &$choices) { | |
$choices[$entity->{'get' . $propertyValue}()] = | |
$entity->{'get' . $property}(); | |
}, $results); | |
return $choices; | |
}; | |
$resolver->setDefaults(array( | |
'class' => null, | |
'choices' => $choices, | |
'property' => null, | |
'property_value' => null, | |
'query_builder' => null, | |
)); | |
} | |
public function getParent() | |
{ | |
return 'choice'; | |
} | |
public function getName() | |
{ | |
return 'doctrineChoice'; | |
} | |
} |
This file contains hidden or 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
services: | |
acme.form.type.doctrine_choice: | |
class: Acme\DemoBundle\Form\Type\DoctrineChoiceType | |
arguments: [@doctrine] | |
tags: [{name: form.type, alias: doctrineChoice}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment