-1

I would like to create a filter where the search would only give me the entries starting with what is searched.

For exemple, I'm searching for a post code starting with "92". With the current filter, it gives me "92000", but also "29200" when I only want the first one.

In SQl it would be "where postcode like $value%" or something like that.

Edit after the 2 first answers:

I tried the custom callback. But the exemple is a for a linked entity displayed as a checked box, and I just want a "where like something%" on a field in the entity. No leftjoin !

Here is my code so far, but I get "Warning: Illegal string offset 'value'"

$this->datagrid->add('postCode', 'doctrine_orm_callback', array(
            'callback' => function($queryBuilder, $field, $value) {
                if (!$value['value']) {
                    return;
                }
                $queryBuilder->andWhere('s.postCode like :value');
                $queryBuilder->setParameter('value', $value);

                return true;
            }
        ));

The field is 'postCode' in the entity 'Trainer'

2 Answers2

0

You need a "custom callback filter" to set the rules you need to the queryBuilder.

It is well covered in Sonata documentation on filters.

np87
  • 543
  • 5
  • 14
0

I had given up on this feature for a while, but then I tried to solve this again, and bam ! it works ! So here is my solution in case anybody needs it:

$this->datagrid->add('postCode', 'doctrine_orm_callback', array(
        'callback' => function($queryBuilder, $alias, $field, $value) {
            if (empty($value['value'])) {
                return;
            }
        $queryBuilder->where('t.postCode LIKE :postcode');
        $queryBuilder->setParameter('postcode', $value['value'].'%');
        $queryBuilder->orderBy('t.postCode', 'ASC');
        return true;
        },
        'field_type' => 'text'
    ));