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