使用LexikFormFilterBundle Symfony2的复杂过滤器

时间:2015-01-15 00:44:39

标签: php symfony

我正在尝试使用此bundle在symfony2中执行复杂过滤器,因此根据以下实体

过滤器表单中的主要实体:

<?php

...

/**
 * LecturaActivo
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class LecturaActivo
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="descripcion", type="string", length=255)
     */
    private $descripcion;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="fecha", type="date", nullable = true)
     */
    private $fecha;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="hora", type="time", nullable=true)
     */
    private $hora;

    /**
     * @ORM\ManyToOne(targetEntity="OperadorActivos", inversedBy="lecturas")
     * @ORM\JoinColumn(name="operador_id", referencedColumnName="id")
     */
    protected $operador;

    /**
     * @ORM\ManyToOne(targetEntity="Activo", inversedBy="lecturas")
     * @ORM\JoinColumn(name="activo_id", referencedColumnName="id")
     */
    protected $activo;

    /**
     * @ORM\ManyToOne(targetEntity="Lista", inversedBy="lecturas")
     * @ORM\JoinColumn(name="lista_id", referencedColumnName="id")
     */
    protected $lista;

    /**
    * @ORM\ManyToOne(targetEntity="\controlid\Bundle\ActivosBundle\Entity\Categoria", inversedBy="lecturas")
    * @ORM\JoinColumn(name="categoria_id", referencedColumnName="id")
    * nullable = true
    */
    protected $categoria;

    /**
    * @var string
    *
    * @ORM\Column(name="posX", type="string", length=255)
    */
    private $posX;

    /**
    * @var string
    *
    * @ORM\Column(name="posY", type="string", length=255)
    */
    private $posY;
    #getters and setters...
}

这是Operadores实体:

<?php

namespace controlid\Bundle\ActivosBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * OperadorActivos
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class OperadorActivos
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="nombres", type="string", length=255)
     */
    private $nombres;

    /**
     * @var string
     *
     * @ORM\Column(name="pin", type="string", length=255)
     */
    private $pin;

    /**
     * @var boolean
     *
     * @ORM\Column(name="activo", type="boolean", nullable=true)
     */
    private $activo;

    /**
     * @ORM\ManyToOne(targetEntity="\controlid\Bundle\CoreBundle\Entity\Usuario", inversedBy="operadoresActivos")
     * @ORM\JoinColumn(name="cliente_id", referencedColumnName="id")
     */
    protected $cliente;

    /**
     * @ORM\OneToMany(targetEntity="LecturaActivo", mappedBy="operador",cascade={"persist", "remove"})
     */
    protected $lecturas;

     /**
     * @ORM\ManyToOne(targetEntity="\controlid\Bundle\ActivosBundle\Entity\Lista", inversedBy="operadores", cascade={"persist", "remove"})
     * @ORM\JoinColumn(name="lista_id", referencedColumnName="id")
     * nullable = true
     */
    protected $lista;

   #getters and setters
}

这是filterType:

<?php

...

class LecturaActivoFilterType extends AbstractType
{


    function __construct($listas, $operadores, $activos, $categorias)
    {
        $this->choicesListas = array(  );
        foreach ($listas as $lista) {
            if(count($lista->getOperadores()) != 0 && count($lista->getActivos())!=0){
                array_push($this->choicesListas, $lista);
            }
        }
        $this->choicesActivos = $activos;
        $this->choicesOperadores =$operadores;
        $this->choicesCategorias =$categorias;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('lista', 'filter_entity', array(
                'class'   =>'controlidActivosBundle:Lista',
                'choices'  =>$this->choicesListas,

                ))
            ->add('operador', 'filter_entity', array('class'=>'controlidActivosBundle:OperadorActivos', 'choices'=>$this->choicesOperadores))
            ->add('categoria', 'filter_entity', array('class'=>'controlidActivosBundle:Categoria', 'choices'=>$this->choicesCategorias))
            ->add('activo', 'filter_entity', array('class'=>'controlidActivosBundle:Activo', 'choices'=>$this->choicesActivos))
            ->add('fecha', 'filter_date_range', array(
                'left_date_options' => array(
                    'widget' => 'single_text',
                    'format' => 'dd-MM-yyyy',
                    'attr' => array('class' => 'date form-box'),
                ),
                'right_date_options' => array(
                   'widget' => 'single_text',
                    'format' => 'dd-MM-yyyy',
                    'attr' => array('class' => 'date form-box'),
                ),
            ));
    }

    public function getName()
    {
        return 'controlid_activo_filer';
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'csrf_protection'   => false,
            'validation_groups' => array('filtering') // avoid NotBlank() constraint-related message
        ));
    }
}

根据上面的实体,我想做一个过滤器我可以做一个过滤器,LecturaActivo->operador等于某个operadorParameter,而operador->cliente是一个clienteParameter 1}}默认情况下,但我不知道如何

任何人都可以帮助我吗?

0 个答案:

没有答案