Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Filtering against multiple choices/entities doesn't work #48

Closed
LordBaine2000 opened this issue Apr 26, 2013 · 11 comments
Closed

Filtering against multiple choices/entities doesn't work #48

LordBaine2000 opened this issue Apr 26, 2013 · 11 comments

Comments

@LordBaine2000
Copy link

Adding the 'multiple' => true option a filter_entity or filter_choice field it causes it to fail when the number of selections is not exactly 1.

If no selections are made I would expect it to ignore the filter, and if more than one is selected I would expect it to include results for both options.

@cedric-g
Copy link
Collaborator

cedric-g commented May 2, 2013

Hi, for the filter_choice it's a forgetting from us, maybe we should check if the value we get is a collection or a single value. But the option should work with the filter_entity except if the relation is a many-to-many, in this case you will have to override the filter by using the apply_filter options

@gaetan-petit
Copy link

@cedric-g : it doesn't seems to work with filter_entity :

Catchable Fatal Error: Argument 1 passed to My\Entity\Class::getProperty() must be an instance of My\Entity\Class, instance of Doctrine\Common\Collections\ArrayCollection given, called in PropertyAccessor.php on line 438

Form code:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('property', 'filter_entity', array(
            'class' => 'MyBundle:MyEntity',
            'expanded' => TRUE,
            'multiple' =>  TRUE,
        ));
    }

@rvanlaak
Copy link
Contributor

That is what Cedric said with its previous comment. You can make use of the 'apply_filter' option, look at the link for an example.

https://github.com/lexik/LexikFormFilterBundle/blob/master/Resources/doc/index.md#a-with-the-apply_filter-option

You should iterate over the results, in order to add it to the QueryBuilder

@gaetan-petit
Copy link

@rvanlaak @cedric-g : I'm sorry but I have read the doc several times and I have absolutely no clue of what I should do with apply_filter option to fix this.

@cedric-g
Copy link
Collaborator

@gaetan-petit do you use a data_class in your type ?
If yes I suppose the field on which you use filter_entity is a single relation, but when you set multiple to true this mean the type will handle a collection (this is the behavior of the entity type). So when the form is submitted and it try to call setProperty() it will pass a collection instead of a single entity.
Just don't defined any data_class and it should work.

@gaetan-petit
Copy link

@cedric-g Thanks for the answer but something is still wrong.

When multiple option are selected the query is not correct, and returns no results:

AND t1_.status_id IN ('43,44,45') 

Instead of:

AND t1_.status_id IN (43,44,45) 

I'am trying to spot where is the faulty code, but I bet you will be faster than me 😉

@cedric-g
Copy link
Collaborator

@gaetan-petit Which version of the bundle are you using ?

@gaetan-petit
Copy link

@cedric-g : I'm on master with in my config.yml

lexik_form_filter:
    where_method: and

@cedric-g
Copy link
Collaborator

I pushed a little change on master, can you try and check if it's ok ?

@gaetan-petit
Copy link

@cedric-g Ha, I was just about to submit the same change in a PR 😛

It works like a charm with the change, thank you 😄

@raphox
Copy link
Contributor

raphox commented Dec 15, 2014

1+
fixed with 49ed731

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants