fork download
  1. <?php
  2. # CONTROLLER METHOD
  3. /**
  4.   * @Route("/", name="foobarIndex")
  5.   */
  6. public function indexAction(){
  7. $repo = $this->getDoctrine()->getRepository('AcmeDemoBundle:Foo');
  8.  
  9. $form = $this->createForm(new \Acme\DemoBundle\Form\FooType($repo));
  10.  
  11. return array(
  12. 'form' => $form->createView()
  13. );
  14. }
  15.  
  16. # FORM TYPE
  17. class FooType extends AbstractType
  18. {
  19. /**
  20.   * @var \Acme\DemoBundle\Entity\FooRepo
  21.   */
  22. private $repository;
  23.  
  24. /**
  25.   * We are going to pass entity repository object but might as well pass data only
  26.   */
  27. function __construct($repository)
  28. {
  29. $this->repository = $repository; // store it, we are going to use it later
  30. }
  31.  
  32. public function buildForm(FormBuilderInterface $builder, array $options)
  33. {
  34. $builder
  35. ->add('someChoiceField', 'choice', array(
  36. 'choices' => $this->repository->someFooMethod() # returns array() of Foo objects
  37. ));
  38.  
  39. # Please make sure to form your array properly,
  40. # e.g. primary key => textual represenation, or at lease have __toString overriden
  41. }
  42.  
  43. public function getName()
  44. {
  45. return 'acme_demobundle_footype';
  46. }
  47. }
  48. ?>
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty