-
Notifications
You must be signed in to change notification settings - Fork 121
Filters and DataTransformer #37
Comments
Hi :) which version of the bundle are you using ? |
I am using your bundle with the jordillonch crud generator: "jordillonch/crud-generator": "2.1.x-dev", |
lexik/form-filter-bundle [dev-master 9c01c14] |
As Then you can define a custom filter class to apply the filter conditions for a <?php
namespace Your\Namespace;
use Doctrine\ORM\QueryBuilder;
use Lexik\Bundle\FormFilterBundle\Filter\ORM\Expr;
use Lexik\Bundle\FormFilterBundle\Filter\ORM\ORMFilter;
class ProductOptionFilter extends ORMFilter
{
/**
* {@inheritdoc}
*/
public function getName()
{
return 'product_option_filter_field'; // this is the same name as ProductOptionFilterFieldType::getName()
}
/**
* {@inheritdoc}
*/
protected function apply(QueryBuilder $queryBuilder, Expr $expr, $field, array $values)
{
if (!empty($values['value'])) {
// add your conditions here
}
}
} You must define this class as a service with a <service id="my_custom_filter.product_option_filter" class="Your\Namespace\ProductOptionFilter">
<tag name="kernel.event_listener" event="lexik_filter.get" method="onFilterGet" />
</service> By doing like this the The |
It works!! Very important to add the transformer_id: $resolver->setDefaults(array(
'invalid_message' => 'The selected productOption does not exist',
'empty_value' => 'Choose an option',
'choices' => $this->buildData(),
'transformer_id' => 'lexik_form_filter.transformer.default',
))
->setAllowedValues(array(
'transformer_id' => array('lexik_form_filter.transformer.default'),
)); Here is the final code: <?php
namespace Ecomm\Bundle\CatalogBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Ecomm\Bundle\CatalogBundle\Form\DataTransformer\ProductOptionFieldTransformer;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ProductOptionFilterFieldType extends AbstractType
{
/**
* @var ServiceContainer
*/
private $sc;
public function __construct(ContainerInterface $container = null)
{
$this->sc = $container;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$transformer = new ProductOptionFieldTransformer($this->sc);
$builder->addModelTransformer($transformer);
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'invalid_message' => 'The selected productOption does not exist',
'empty_value' => 'Choose an option',
'choices' => $this->buildData(),
'transformer_id' => 'lexik_form_filter.transformer.default',
))
->setAllowedValues(array(
'transformer_id' => array('lexik_form_filter.transformer.default'),
));
}
private function buildData()
{
$em = $this->sc->get('doctrine')->getManager();
$request = $this->sc->get('request');
$choices = array();
$productOptions = $em
->getRepository('EcommCatalogBundle:ProductOption')
->createQueryBuilder('c')
->select('c, ct')
->innerJoin('c.translations', 'ct')
->andWhere('ct.locale = :locale')
->setParameter('locale', $request->getLocale())
->orderBy('ct.name', 'ASC')
->getQuery()
->getResult();
foreach ($productOptions as $productOption)
{
// I assume key is retrieved by getId
$translation = $productOption->getTranslations();
$choices[$productOption->getId()] = $translation[0]->getName();
}
return $choices;
}
public function getName()
{
return 'product_option_filter_field';
}
public function getParent()
{
return 'choice';
}
}
<?php
namespace Ecomm\Bundle\CatalogBundle\Form;
use Doctrine\ORM\QueryBuilder;
use Lexik\Bundle\FormFilterBundle\Filter\ORM\Expr;
use Lexik\Bundle\FormFilterBundle\Filter\ORM\ORMFilter;
class ProductOptionFilter extends ORMFilter
{
/**
* {@inheritdoc}
*/
public function getName()
{
return 'product_option_filter_field'; // this is the same name as ProductOptionFilterFieldType::getName()
}
/**
* {@inheritdoc}
*/
protected function apply(QueryBuilder $queryBuilder, Expr $expr, $field, array $values)
{
if (!empty($values['value']))
{
// add the join if you need it and it not already added
$alias = $queryBuilder->getRootAliases();
$queryBuilder->innerJoin($alias[0].'.productOption', 'po');
$queryBuilder->andWhere('po.id = :id')
->setParameter('id', $values['value']);
}
}
} |
Hi,
I am using your Bundle for my application. I am migrating a symfony 1.4 website to a symfony 2.1 and I have the following problem with the translations, since the doctrine extensions for 1.4 and 2.1 have a different approach. I have two entities, ProductOption (ProductOptionTranslation to keep the translation) and ProductOptionValue (ProductOptionValueTranslation to do the same as before). The relationship between ProductOption and ProductOptionValue is 1:N (i.e Chocolate/Strawberry/Vanilla are Flavours)
What I want is to filter in the index view of the ProductOptionValue by ProductOption, but the name of the ProductOption is in ProductOptionTranslation entity. So I created a new choice type with a datatransformer. The filter form show the choice perfectly, but the filter is not applied because in the code above the form does not have any children since I am using a DataTransformer and not a filter_XXXX field.
protected function addFilters(FormInterface $form, $filterBuilder, $alias = null, array &$parts = array(), $expr = null)
{
/** @var $child FormInterface */
foreach ($form->all() as $child) {
Here is the code:
The text was updated successfully, but these errors were encountered: