2016-03-24 7 views
0

私はLexikFormFilterBundleを使用してエンティティとその関係をフィルタリングしています。私はapply_filterクロージャを追加しようとしているので、クエリが実行される前にフィルタを持っているフィールドの値を変更できます。閉鎖は私が何をしても決して呼ばれません。皆さん、私を助けてください、何が起こっているのか教えてください。ありがとう。symfony apply_filterがlexikフォームフィルタで動作しません

TimesheetController.php

<?php 

    namespace Homecare\HomecareBundle\Controller; 

    use Homecare\HomecareBundle\Form\TimesheetFilterType; 
    use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
    use Homecare\HomecareBundle\Entity\CareGoals; 
    use Homecare\HomecareBundle\Entity\User; 
    use Symfony\Component\HttpFoundation\Request; 
    use Symfony\Component\HttpFoundation\Response; 
    use Symfony\Component\HttpFoundation\Session\Session; 

    class TimesheetController extends Controller 
    { 


     public function indexAction(Request $request) 
     { 

      $form = $this->createForm(new TimesheetFilterType(), null, [ 
       'action' => $this->generateUrl('Homecare_HomecareBundle_dashboard_timesheets'), 
       'method' => 'GET', 
      ]); 


      $filterBuilder = $this->getDoctrine()->getManager()->getRepository(
       'HomecareHomecareBundle:Timesheet' 
      )->createQueryBuilder('t'); 
      //->join('t.recipient', 'r'); 

      $form->handleRequest($request); 

      if ($form->isValid()) { 

       $this->get('lexik_form_filter.query_builder_updater') 
        ->addFilterConditions($form, $filterBuilder); 

       $timesheets = $filterBuilder->getQuery()->getResult(); 


       // dump($filterBuilder->getDql()); 
      } 


      $timesheets = $filterBuilder->getQuery()->getResult(); 

      return $this->render(
       'HomecareHomecareBundle:Timesheet:index.html.twig', 
       array(
        'form' => $form->createView(), 
        'timesheets' => $timesheets 
       ) 
      ); 

     } 

} 

TimesheetFilterType.php

<?php 
/** 
* Created by PhpStorm. 
* User: joshuacrawmer 
* Date: 3/23/16 
* Time: 4:49 PM 
*/ 

namespace Homecare\HomecareBundle\Form; 

use Ambta\DoctrineEncryptBundle\Encryptors\Rijndael256Encryptor; 
use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolverInterface; 
use Doctrine\ORM\Query\Expr; 
use Doctrine\ORM\QueryBuilder; 
use Lexik\Bundle\FormFilterBundle\Filter\FilterBuilderExecuterInterface; 
use Lexik\Bundle\FormFilterBundle\Filter\Form\Type as Filters; 
use Lexik\Bundle\FormFilterBundle\Filter\Query\QueryInterface; 


class TimesheetFilterType extends AbstractType 
{ 

    /** 
    * @param FormBuilderInterface $builder 
    * @param array $options 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 


     $builder->add('recipient', Filters\CollectionAdapterFilterType::class, array(
      'entry_type'  => new RecipientFilterType(), 
      'label' => false, 
      'add_shared' => function (FilterBuilderExecuterInterface $qbe) { 

       $closure = function (QueryBuilder $filterBuilder, $alias, $joinAlias, Expr $expr) { 
        // add the join clause to the doctrine query builder 
        // the where clause for the label and color fields will be added automatically with the right alias later by the Lexik\Filter\QueryBuilderUpdater 
        $filterBuilder->join($alias . '.recipient', $joinAlias); 
        //$filterBuilder->join($alias . '.options', $joinAlias); 
       }; 



       // then use the query builder executor to define the join and its alias. 
       $qbe->addOnce($qbe->getAlias().'.recipient', 'r', $closure); 
      }, 
      'apply_filter' => function (QueryInterface $filterQuery, $field, $values) { 
       $encryptor = new Rijndael256Encryptor('ABCDEFGHIJKLMNOPQRSTUVWXZY123456'); 

       if (empty($values['value'])) { 
        return null; 
       } 

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

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

       // expression parameters 
       $parameters = array($paramName => $encryptor->encrypt($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); 


      }, 
     )); 


    } 

    /** 
    * @param OptionsResolverInterface $resolver 
    */ 
    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
     $resolver->setDefaults(array(
      'csrf_protection' => false, 
     )); 
    } 

    /** 
    * @return string 
    */ 
    public function getName() 
    { 
     return 'timesheet_filter'; 
    } 

} 

答えて

0

私はみんなそれを考え出しました。 'apply_filter'は実際に関連するフォームタイプに追加する必要があります。その後、それは動作します。

関連する問題