Code Monkey home page Code Monkey logo

Comments (7)

slavik112211 avatar slavik112211 commented on July 21, 2024

As of now, I've solved the problem with a custom filtering type like this:

https://gist.github.com/3901055

and then, I'm defining the field in the form as follows:
->add('company', 'filter_entity_name',array('label' => "Company", 'association_field' => "c.name"))

As you see, the code is using the
Symfony\Component\Form\FormInterface $form, which represents the field.

It's not transferred from
Lexik\Bundle\FormFilterBundle\Filter\QueryBuilderUpdater#applyFilterCondition(),
so I had to extend that class.

It would be much preferable to receive that parameter, 'association_field' as part of the $values array.

Regards

from lexikformfilterbundle.

slavik112211 avatar slavik112211 commented on July 21, 2024

Found a better way to achieve the filtering of associated data:

I've defined also the custom Transformer, that my custom type is using to transform incoming values from formField and transmits the values to $values of applyFilter:

CustomType: https://gist.github.com/3901221

Transformer: https://gist.github.com/3901225

Configuring services: https://gist.github.com/3901235

Using: ->add('company', 'filter_entity_name',array('label' => "Company", 'association_field' => "c.name"))

Overall, I'm happy with what is given out of the box with Lexik form filter, like saving filters in session etc,
but problems like this make you wondering why would you use a third-party library in the first place.

from lexikformfilterbundle.

cedric-g avatar cedric-g commented on July 21, 2024

Hi, did you try to override the default way to apply the condition with the apply_filter option ? https://github.com/lexik/LexikFormFilterBundle/blob/master/Resources/doc/index.md#override-default-way-to-apply-the-filter

from lexikformfilterbundle.

slavik112211 avatar slavik112211 commented on July 21, 2024

Hey Cedric,

AFAIK, this would allow us to override the ApplyFilter method for some existing filtering type, say filter_text.

In my case, I needed to not only override the ApplyFilter method, but to transmit the name of the text field of a DoctrineQL query as part of a array $values parameter:
->add('company', 'filter_entity_name',array('label' => "Company", 'association_field' => "c.name"))
(filter the users by the associated company, using it's c.name textfield)

For that, I have overridden the Transformer used to work on a text field properties.
I'm afraid overriding the Transformer is not possible with the apply_filter

from lexikformfilterbundle.

cedric-g avatar cedric-g commented on July 21, 2024

In the following exemple I use the v1.1.1.

By using the apply_filter option, I think you can do something like:

$builder->add('company', 'filter_text', array(
    'apply_filter' => function (QueryBuilder $queryBuilder, Expr $expr, $field, array $values) {
        if (!empty($values['value'])) {
            // add the join if you need it and it not already added
            // $queryBuilder->leftJoin('u.company', 'c');

            $queryBuilder->andWhere('c.name = :name')
                ->setParameter('name', $values['value']);
        }
    },
));

Another way to do it would be to embed a CompanyFilterType in your UserFilterType which is a bit better in case of you want to filter on a field which belong to a related entity.

class UserFilterType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // ...
        $builder->add('company', new CompanyFilterType());
    }

    public function getName()
    {
        return 'user_filter';
    }
}
class CompanyFilterType extends AbstractType implements FilterTypeSharedableInterface
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', 'filter_text');
    }

    public function getName()
    {
        return 'company_filter';
    }

    public function addShared(QueryBuilderExecuterInterface $qbe)
    {
        $qbe->addOnce($qbe->getAlias().'.company', 'c', function(QueryBuilder $queryBuilder, $alias, $joinAlias, Expr $expr) {
            $queryBuilder->leftJoin($alias . '.company', 'c');
        });
    }
}

Note that if your doctrine query builder is already initialized with the join you don't need to add it in the CompanyFilterType::addShared(), but you will have to set the alias on the query builder update before calling theaddFilterConditions method.
$this->get('lexik_form_filter.query_builder_updater')->setParts(array('u.company' => 'c'));

from lexikformfilterbundle.

slavik112211 avatar slavik112211 commented on July 21, 2024

Hey Cedric,

I've used the nested forms approach, works perfectly, thanks!
Feel bad I have not found the documentation on nested forms at first.
I would suggest you to have the topic of filtering by associated data being stated more boldly in documentation,
as this functionality is crucial for non-trivial filtering.

Best Regards,
Slavik

from lexikformfilterbundle.

rvanlaak avatar rvanlaak commented on July 21, 2024

Agree with the last comment, especially the addJoin works nice and should be documented 👍

from lexikformfilterbundle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.