mercredi 1 mai 2019

symfony - filter manytomany with multiple checkboxes

I want to filter results based on manytomany relation with multiple checkboxes.

I have 2 entities. 1) "properties" 2) "views". Where in properties a view arraycollection is being added with a manytomany field based on view entity.

Now I made a multiplecheckbox form based on what values views has (i made it manuel for now). This shows up in the form. However i don't know how to filter the results based on the checkboxes.

As hmtl formoutput i get the following multiple input fields with an array element

<input type="checkbox" name="form[viewchoices][]" value=1">
<input type="checkbox" name="form[viewchoices][]" value=2">
<input type="checkbox" name="form[viewchoices][]" value=2">

I now try to filter in the controller with query. First get the data

    $view = array();
    $view["viewchoices"] = $form["viewchoices"]->getData();

and then i try to do the filter

        $pagination->innerJoin('a.propviews', 'd')
                ->addSelect('d');

        // Filter results using orWhere matching ID
        foreach ($view['viewchoices'] as $view)
        {
            $pagination->andWhere('d.id like :viewchoices');
             ->setParameter('id', $view);
        }

I render the output with knp paginator. When i try to filter i get an error 'Too few parameters: the query defines 5 parameters but you only bound 4'. But i don't think my code is optimal. Any ideas how to do this? Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire