1

I have this code

->add('user', 'entity', array(
                            'class' => 'Acme\Entity\User',
                            'query_builder' => function(EntityRepository $er) use ($options)
     {                        return $er->createQueryBuilder('u')
                               ->orderBy('u.name', 'ASC');
                            },
            'data' => $option['id']
            ))

Its not working

public function buildForm(FormBuilderInterface $builder, array $options)


    {
            $builder
              ->add('description')

              ->add('user', 'entity', array(
                'class' => 'Acme\Entity\User',
                'query_builder' => function(EntityRepository $er) use ($options) {
                    return $er->createQueryBuilder('u');

                },
              'preferred_choices' => array('2')
              ))
        ;
    }
Mirage
  • 30,868
  • 62
  • 166
  • 261
  • Maybe will help you: http://stackoverflow.com/questions/8073236/symfony2-setting-a-default-choice-field-selection – Mikhail Aug 17 '12 at 05:03
  • Well , i already tried that but it didn't tell when i get data from database. For array it work but its not working with query builder – Mirage Aug 17 '12 at 05:15

2 Answers2

5

You can use the one of the following:

  1. Set a default value in the object

    $cl->setUser($this->getDoctrine()->getEntityManager()->getReference('Acme:User',2));
    
  2. Use a preferred choices option in the form builder:

    'preferred_choices' => array('2')
    
  3. Or set 'property_path' => false and use a 'data' => YourDefaultEnity

Mikhail
  • 2,542
  • 4
  • 29
  • 40
  • 1. I can't have default value because i ma creating the user tasks and passing the userid from userpage. so depending upon the userpage in my tasks page i will have that user already setup in sleectbox. 2. REagding `prefered_choice` , it says that `The option "preferred_choice" does not exist.` – Mirage Aug 17 '12 at 05:40
  • Sorry my typo. updated. And please provide a more info (controller, form builder, view) to help you – Mikhail Aug 17 '12 at 05:43
  • i now get this error with choices `Warning: spl_object_hash() expects parameter 1 to be object, string given in /home/randhir/public_html/Kites/Symfony/vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php line 88`. There is nothing special in the code. It was craeted by doctrine itself. i will add the form calss in my ques – Mirage Aug 17 '12 at 06:12
  • How you call a form creation: `$form = $this->createForm(new YourType)` or `$form = $this->createForm(new YourType, new SomeObject)` ? – Mikhail Aug 17 '12 at 06:27
  • `$form = $this->createForm(new MyType(), $entity,array('id' => $id));` – Mirage Aug 17 '12 at 06:48
  • So `$entity` object contains a `description` and `user` fields. Right? – Mikhail Aug 17 '12 at 06:59
  • So, as I say, you need to: `$entity->setUser($this->getDoctrine()->getRepository('Acme:User')->find($id)); $form = $this->createForm(new MyType(), $entity);` – Mikhail Aug 17 '12 at 08:33
0

The form should map the user->id value automatically to the selected entity select field. For example if your have a Computer entity that has a OnetoOne relationship with a User entity in a join table called 'computer_users':

class Computer{

    private $user;

    /**
    * @ORM\OneToOne(targetEntity="ComputerUser", mappedBy="computer")
    */
    private $computerUsers;

    public function getUser(){
       return $computerUsers->getUser();
    }

    private function getComputerUser(){
       return $this->$computerUsers;
    }
}

The field 'user' in the form class should pick up the user->id value from the 'user' attribute object in the Computer class passed to the form.

Alternatively, you can explicitly set the value by explicitly passing the user entity into the form using SetData():

    $computerForm = $this->createForm( new ComputerForm(), $computer );
    $user         = $computer->getComputerUser()->getUser();

    $computerForm->get('user')->setData( $user );

    return $this->render( 'AcmeBundle:Computer:edit.html.twig'
                        , array( 'computer' => $computer
                        , 'computerForm'    => $computerForm->createView()
            )
    );
Onshop
  • 2,915
  • 1
  • 27
  • 17