Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify what value is passed using a choice form field in Symfony2

I have the following code which displays all the available main pages that can be used when adding sub pages in my project:

        $builder->add('subtocontentid',
          'entity',
           array(
                 'class'=>'Shout\AdminBundle\Entity\Content',
                 'property'=>'title',
                 'query_builder' => function (EntityRepository $repository)
                 {
                     return $repository->createQueryBuilder('s')
                            ->where('s.mainpage = ?1')
                            ->setParameter(1, '1')
                            ->add('orderBy', 's.created ASC');
                 }
        ));

In the form, it works alright. It displays the proper title of the mainpage. However, when the form is passed to the database, the page ID is passed to the database. This isn't how I want it to work, I need it to pass on a slug to the database instead.

I understand that the code I'm using retrieves all of the fields in the database. How could I select just the Title field and the Slug field, and then in the form pass the Slug field to the database?

Cheers

like image 253
mickburkejnr Avatar asked Sep 23 '11 16:09

mickburkejnr


1 Answers

You will have to change the Transformer used by EntityType, And it isnt the id being passed to the database it is the entity as the Transformer takes the id and looks up the entity in its list. So in your case it would be an instance of Shout\AdminBundle\Entity\Content

like image 133
Henrik Bjørnskov Avatar answered Oct 08 '22 00:10

Henrik Bjørnskov