I have a table as below:
items
-id (pk) AI
-user_id (fk) references users
-title
-description
I have a form page which requires authenticated users. I want to fill the select box with items that belongs to the user (like select * from items where user_id=loggedinUserId)
I looked at the documentation and I found this:
$builder->add('items', 'entity', array(
'class' => 'AcmeHelloBundle:Items',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('i')
->orderBy('i.title', 'ASC');
},
));
My question is how can I pass the authenticated user id to this query in symfony 2.1 forms ?
You have two possibilities:
This awesome thread show you an interesting way of doing it (from @khepin). However, as suggested by @Bernhard (see first comment), there is a simpler way in that case.
METHOD 1- Constructor Injection : If you can’t bother creating a suscriber etc… you can directly inject the security context into your form constructor :
ItemType:
Declare it as a service:
To build your form, you can now do :