Comments (7)
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.
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.
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.
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.
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.
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.
Agree with the last comment, especially the addJoin
works nice and should be documented 👍
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.