Symfony: use of query builder in a Form
Asked Answered
C

2

6

I have a form that I am building using the FormBuilder:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('rosters',   'entity',   array(
            'class' => 'ReliefAppsPlatformBundle:Roster',
            'property' => 'display',
            'query_builder' => function(RosterRepository $r) use ($user) {
                return $r->createQueryBuilder('r')
                    ->leftJoin('r.members', 'm')
                    ->addSelect('m')
                    ->where('(m.rights = :adminRight or m.rights = :managerRight) and m.user = :user')
                    ->setParameter('adminRight', RosterMember::ADMIN_LEVEL)
                    ->setParameter('managerRight', RosterMember::MANAGER_LEVEL)
                    ->setParameter('user', $user);
            },
            'required' => true,
            'expanded' => true,
            'multiple' => true
        ))
        ->add('save', 'submit')
    ;
}

As you can see I my QueryBuilder I use $user (the current user) as a parameter. My controler looks like that:

public function createAction(Request $request, Event $event)
{
    $alert = new RosterEvent;
    $alert->setEvent($event);

    $user = $this->getUser();
    $form = $this->createForm(new RosterEventType(), $alert, $user);
    $form->handleRequest($request);
    if ($form->isValid()) { ....

My issue is that I need to pass the $user to the formbiulder. But I get "Catchable Fatal Error: Argument 3 passed to Symfony\Bundle\FrameworkBundle\Controller\Controller::createForm() must be of the type array, object given,..." I know the problem is how I pass $user from the controller to the formbuilder. But I have no clue how to do that. Any ideas?

Craniate answered 15/7, 2015 at 13:52 Comment(3)
try passing the id of the user as follow: $user->getId()Transfusion
try ->setParameter('user', $user->getId());Transfusion
Thanks @Matteo; I tried but I get Catchable Fatal Error: Argument 3 passed to Symfony\Bundle\FrameworkBundle\Controller\Controller::createForm() must be of the type array, integer given,Craniate
S
5

As mentioned in the documentation, the third parameter ($options = array()) of method createForm need an array and not a object.

This line is wrong

$form = $this->createForm(new RosterEventType(), $alert, $user);

The $options parameter can be used for example like this

$form = $this->createForm(new TaskType(), $task, array(
    'action' => $this->generateUrl('target_route'),
    'method' => 'GET',
));

If you want pass a parameter to the form, you can try something like this

Controller

$form = $this->createForm(new RosterEventType($user), $alert);

Form

protected $user;

public function __construct (User $user)
{
    $this->user = $user;
}

Hope it will help.

Saturninasaturnine answered 15/7, 2015 at 14:44 Comment(1)
Yes, that the solution, I found it yesterday but thanks a lot. Also it is necessary to __construst the variable in the RosterEventType.php (as follows): private $userId; public function __construct ($userId) { $this->userId = $userId; }Craniate
A
2

The third parameter of createForm is expecting an array of options so use this when creating the form:

$form = $this->createForm(new RosterEventType(), $alert, array('user'=>$user));

Then you will need to get your RosterEventType to allow the 'user' option to be provided. You can do this by overriding the setDefaultOptions function of your AbstractType and provided a default value for the option. For example you can add the following to your RosterEventType class:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'user' => false,
    ));
}

If you do this then you can access the user object in your buildForm function using $options['user'].

Abner answered 15/7, 2015 at 14:41 Comment(2)
Well done ! ;) Just for more information you can add in your post that the setDefaultOptions was replaced by configureOptions in Symfony 2.7Saturninasaturnine
Hi I accepted @Saturninasaturnine answer as it is the same I am using but yours is also very good. Thanks a lot!!Craniate

© 2022 - 2024 — McMap. All rights reserved.