-
-
Save baldurrensch/4659886 to your computer and use it in GitHub Desktop.
Revisions
-
merk revised this gist
Jan 28, 2013 . 1 changed file with 16 additions and 25 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -2,9 +2,9 @@ namespace Ibms\AppBundle\Form\EventListener; use Doctrine\ORM\QueryBuilder; use Ibms\SupplierBundle\Entity\Supplier; use Ibms\SupplierBundle\Entity\SupplierManager; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\FormFactoryInterface; @@ -15,56 +15,47 @@ class ApplianceTypeListener implements EventSubscriberInterface { private $factory; private $fieldName; private $supplierManager; public function __construct($fieldName, FormFactoryInterface $factory, SupplierManager $supplierManager) { $this->factory = $factory; $this->fieldName = $fieldName; $this->supplierManager = $supplierManager; } public function preBind(FormEvent $event) { $form = $event->getForm(); $data = $event->getData(); $qb = $this->supplierManager->queryApplianceTypes(); $qb->setParameter('supplierId', $data['supplier']); $this->addApplianceType($form, $qb); } public function preSetData(FormEvent $event) { $form = $event->getForm(); $data = $event->getData(); if (null === $data or null === $data->getSupplier()) { return; } $qb = $this->supplierManager->queryApplianceTypes($data->getSupplier()); $this->addApplianceType($form, $qb); } protected function addApplianceType(FormInterface $form, QueryBuilder $qb) { $form->add($this->factory->createNamed($this->fieldName, 'entity', null, array( 'class' => 'Ibms\\SupplierBundle\\Entity\\ApplianceType', 'empty_value' => 'Pick one', 'property' => 'type', 'required' => false, 'query_builder' => $qb, ))); } -
merk created this gist
Sep 28, 2012 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,78 @@ <?php namespace Ibms\AppBundle\Form\EventListener; use Doctrine\ORM\EntityRepository; use Doctrine\ORM\Query\Expr\From; use Ibms\SupplierBundle\Entity\Supplier; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\FormInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class ApplianceTypeListener implements EventSubscriberInterface { private $factory; private $fieldName; public function __construct($fieldName, FormFactoryInterface $factory) { $this->factory = $factory; $this->fieldName = $fieldName; } public function preBind(FormEvent $event) { $form = $event->getForm(); $data = $event->getData(); $this->addApplianceType($form, $data['supplier']); } public function preSetData(FormEvent $event) { $form = $event->getForm(); $data = $event->getData(); if (null === $data) { return; } $supplierId = $data->getSupplier() ? $data->getSupplier()->getId() : null; $this->addApplianceType($form, $supplierId); } protected function addApplianceType(FormInterface $form, $supplierId) { $form->add($this->factory->createNamed($this->fieldName, 'entity', null, array( 'class' => 'Ibms\\SupplierBundle\\Entity\\ApplianceType', 'empty_value' => 'Pick one', 'property' => 'type', 'required' => false, 'query_builder' => function (EntityRepository $er) use ($supplierId) { $qb = $er->createQueryBuilder('a'); $qb->add('from', new From('Ibms\\SupplierBundle\\Entity\\Supplier', 's'), true) ->andWhere('a MEMBER OF s.applianceTypes') ->andWhere('s.id = :supplierId'); $qb->setParameters(array( 'supplierId' => $supplierId, )); return $qb; } ))); } public static function getSubscribedEvents() { return array( FormEvents::PRE_BIND => 'preBind', FormEvents::PRE_SET_DATA => 'preSetData' ); } } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ <?php class ConsumingType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { // ... // Handles adding a dropdown for Type given the forms set data $builder->addEventSubscriber(new ApplianceTypeListener('type', $builder->getFormFactory())); } }