Comments (8)
This helped me a lot. Here is an example of using the 'apply_filters' option with an entity filter to allow a user to select multiple options on a many-to-many relationship from a multi-select box and filter the results.
->add('myEntity', 'filter_entity', array(
'class' => 'AppMyBundle:MyEntity',
'expanded' => false,
'multiple' => true,
'apply_filter' => function (QueryInterface $filterQuery, $field, $values)
{
$query = $filterQuery->getQueryBuilder();
$query->leftJoin($field, 'm');
// Filter results using orWhere matching ID
foreach ($values['value'] as $value)
{
$query->orWhere($query->expr()->in('m.id', $value->getId()));
}
},
))
from lexikformfilterbundle.
By default the filter_entity
don't work with many to many and I don't think we will make it able to check the relation type. In this case you will have to use the apply_filter
option to change the way the condition is applied on the query (as you saw it in #25).
from lexikformfilterbundle.
Still can't get manyToMany
relationship filtering working. It seems that this has something to do with the way how Doctrine filters manyToMany
relations.
http://stackoverflow.com/a/8959837/1794894
I've been looking in your apply_filter
functionality, but can't get it to work. Probably this is because my manyToMany relationship does not have a Doctrine entity class.
$filterBuilder->leftJoin('BundleName\Language', 'l');
$filterBuilder->andWhere($expr->inEq('l.id', $values['value']));
ladybug_dump($filterBuilder->getQuery()->getSQL());
Above code snippet does not filter the results correct, since the link table is not used. Can you please provide a working example on manyToMany filtering?
from lexikformfilterbundle.
Let's say you have a many to many between User
and Language
, so in User
you should have a languages
property. If you want to filter your users by languages, you can do something like:
$filterBuilder->leftJoin('u.languages', 'l');
$filterBuilder->andWhere($expr->in('l.id', $values['value'])); // here $values['value'] will be a collection of objects so maybe you will have to transform it into an array of ids to make the `in` expression work correctly.
from lexikformfilterbundle.
Ah great, finally got it working! Some important remarks from my side:
- You don't have to join the entity (in above examle Language), but the relationship itself. What I did is leftJoin
Language
but this was an incorrect join. This indeed should beu.languages
. - In manyToMany relationships the filter values indeed need to be transformed in order to, for example make an 'in' expression work. So, $values['value'] contains an ArrayCollection with the selected objects, but the query needs ID's. Loop through the ArrayCollection to manually format the parameter for in the filterBuilder
from lexikformfilterbundle.
Hi, thanks for this. This is a version with one value (multiple = false)
$builder->add('myEntity', 'filter_entity', array(
'class' => 'AppMyBundle:MyEntity',
'expanded' => false,
'multiple' => false,
'apply_filter' => function (QueryInterface $filterQuery, $field, $values)
{
$query = $filterQuery->getQueryBuilder();
$query->leftJoin($field, 't');
$value = $values['value'];
if (isset($value)){
$query->andWhere($query->expr()->in('t.id', $value->getId()));
}
}));
from lexikformfilterbundle.
I did exactly the same but didn't work to me. The problem was that the andWhere don't add the filter at the query, so I did this:
$builder->add('myEntity', 'filter_entity', array(
'class' => 'AppMyBundle:MyEntity',
'expanded' => false,
'multiple' => false,
'apply_filter' => function (QueryInterface $filterQuery, $field, $values)
{
$query = $filterQuery->getQueryBuilder();
$query->innerJoin($field, 't');
$value = $values['value'];
if (isset($value)){
$paramName = sprintf('p_%t', str_replace('.', '_', $field));
$expression = $filterQuery->getExpr()->eq("t.id", ':'.$paramName);
$parameters = array($paramName => $values['value']->getId());
return $filterQuery->createCondition($expression, $parameters);
}
}));
I changed to innerJoin because I want to filter with and and if you use left join it will no work properly.
from lexikformfilterbundle.
@franciscomemoli I think you and @djoo are just not using the same version of the bundle. The $filterQuery->createCondition(...)
is only available since the version 3.x
.
from lexikformfilterbundle.
Related Issues (20)
- Mongo as separate packages? HOT 2
- v5.0.9 Broken with symfony 3.4 HOT 2
- Service "lexik_form_filter.query_builder_updater" not found HOT 4
- doctrine/orm v2.6.4 has issues with using ChoiceFilterType with objects
- NumberRangeFilterType can not set attribute " type:'number' '' , and 'label' is not work as well HOT 3
- CollectionAdapterFilterType display an unwanted "level" with a label : "0" HOT 2
- Store Entity in session HOT 1
- Not a issue, change repo description HOT 1
- How to have more user/search engines friendly urls
- Works in symfony 5.1 ? HOT 1
- How to add a field to set maxresults HOT 1
- Options 'data' does not work for EntityFilterType
- Hi HOT 3
- Undefined class constant 'INTEGER' when filtering EntityFilterType HOT 2
- doctrine/orm to specific HOT 1
- Using ChoiceFilterType generate: Undefined constant Doctrine\DBAL\Types\Type::STRING HOT 1
- Ltrim issue in LexikFormFilterBundle/Event/Subscriber/DoctrineORMSubscriber.php
- Filter by entity with string id HOT 3
- Feature Request: Allow using array based $parameters with IN() expressions HOT 2
- Bundle abandonned...and moved on new organization!
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lexikformfilterbundle.