简体   繁体   中英

Symfony2 LexikFormFilter bundle custom filter

I have an entity named Product with a name property.

For a product named "Red Light Saber" for example, I would like to be able to get it as a result when I type "Saber" or "Red" in the filter text field.

But actually, I need to type exactly "Red Light Saber" for the product to appears.

I'm using LexikFormFilterBundle . To solve my problem, I try to do what the documentation explain in section iv. Filter customization .

This my ProductFilterType.php

<?php

namespace ProjectList\Bundle\ProductBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormBuilder;
use Doctrine\ORM\QueryBuilder;
use Lexik\Bundle\FormFilterBundle\Filter\Query\QueryInterface;

class ProductFilterType extends AbstractType
{
    public $choices;

    public function __construct($providers)
    {
        foreach ($providers as $provider) {
            $this->choices[$provider->getId()] = $provider->getName();
        }
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', 'filter_text', array('apply_filter' => function (QueryInterface $filterQuery, $field, $values)
            {
                if (empty($values['value'])) {
                    return null;
                }

                $paramName = sprintf('p_%s', str_replace('.', '_', $field));

                // expression that represent the condition
                $expression = $filterQuery->getExpr()->eq($field, ':' . $paramName);
                var_dump($expression);
                die;

                // expression parameters
                $parameters = array($paramName => $values['value']); // [ name => value ]
                // or if you need to define the parameter's type
                // $parameters = array($paramName => array($values['value'], \PDO::PARAM_STR)); // [ name => [value, type] ]

                return $filterQuery->createCondition($expression, $parameters);
            },
            ))
            ->add('description', 'filter_text')
            ->add('type', 'filter_choice', array(
                'choices' => array(0 => 'Gadget', 1 => 'Textile')
            ))
            ->add('provider', 'filter_choice', array(
                'choices' => $this->choices,
            ));

        $listener = function (FormEvent $event) {
            // Is data empty?
            foreach ($event->getData() as $data) {
                if (is_array($data)) {
                    foreach ($data as $subData) {
                        if (!empty($subData)) return;
                    }
                } else {
                    if (!empty($data)) return;
                }
            }

            $event->getForm()->addError(new FormError('Filter empty'));
        };
        $builder->addEventListener(FormEvents::POST_BIND, $listener);
    }

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

This is my var_dump($expression) of line 40 result :

object(Doctrine\ORM\Query\Expr\Comparison)[620]
  protected 'leftExpr' => string 'e.name' (length=6)
  protected 'operator' => string '=' (length=1)
  protected 'rightExpr' => string ':p_e_name' (length=9)

I think I have to change the operator for something like contain but I don't know how.

If you want to act as a SQL LIKE statement you simply do as follow:

        ->add('name', 'filter_text', 'filter_text', array('condition_pattern' => FilterOperands::STRING_BOTH));

As you can see in the source code here and described here in the doc

Hope this help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM