drbii0359 2016-02-19 13:22
浏览 45
已采纳

Symfony2 - 从ORM DQL中设置表单中复选框的值

I'm creating a form that is supposed to filter a custom query list in Symfony2(ver 2.3) using LexikFilter Bundle.
Project setup:

  1. Symfony2 :2.3.7
  2. LexikFilter Bundle : 3.0.8
  3. Doctrine ORM 2.4

The project consist of entities having a n:m relationship (Parents-Training) , the Parent Filter Form embed a Training filter form as below:

class MyParentsType extends AbstractType
{

    private $repository ;

    function __construct($repository)
    {
        $this->repository = $repository; // store it, we are going to use it later
    }

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

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('Firstname', 'filter_text', array('condition_pattern' => FilterOperands::STRING_BOTH,))
                ->add('Lastname', 'filter_text', array('condition_pattern' => FilterOperands::STRING_BOTH,))
                ->add('trainings','filter_collection_adapter', array(
                                            'type'=> new MyTrainingType2($this->repository),
                                            'default_data' => new ArrayCollection(),
                                            'add_shared'=> function(FilterBuilderExecuterInterface $qbe){
                                                $closure = function(QueryBuilder $filterBuilder, $alias, $joinAlias, Expr $expr){
                                                     $filterBuilder ->leftJoin($alias.'.options',$joinAlias);
                                                };
                                                $qbe->addOnce($qbe->getAlias().'.options','opt',$closure);
                                            },
                ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(
            [
                'data_class' =>'tuto\LexikTestBundle\Entity\Parents',
                'csrf_protection' => false,
                'validation_groups' => ['filtering'] // avoid NotBlank() constraint-related message
            ]
        );
    }

and the Training filter form:

class MyTrainingType2 extends AbstractType
{

    private $repository ;

    function __construct($repository)
    {
        $this->repository = $repository; // store it, we are going to use it later
    }


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

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('Title', 'filter_text', array('condition_pattern' => FilterOperands::STRING_BOTH,))
                ->add('Location', 'filter_choice', array( 'choice_list' => new ChoiceList($this->repository->getLocationList(),$this->repository->getLocationList()),
                                                          'expanded'=> true,
                                                          'multiple'=>true,
                                                          )
                    );

    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(
            [
                'data_class' =>'tuto\LexikTestBundle\Entity\Training',
                'csrf_protection' => false,
                'validation_groups' => ['filtering'] // avoid NotBlank() constraint-related message
            ]
        );
    }
}  

Here is the getLocationList() method of the Training Repository:

 public function getLocationList()
    {   $qb = $this->createQueryBuilder('t')
        ->select('t.Location')//
        ->orderBy('t.Location','asc')
        ->distinct(true);

        return $qb->getQuery()->getArrayResult();
    }

The filter does display the checkboxes but the value field is set to the column name Location. Here a snippet of the array being returned by the getArrayResult

array (size=199)
  0 => 
    array (size=1)
      'Location' => string '‘Arīshah' (length=11)
  1 => 
    array (size=1)
      'Location' => string 'Abuja' (length=5)

I would like to have it returned as :

 0 => 
    array (size=1)
      '‘Arīshah' => string '‘Arīshah' (length=11)
  1 => 
    array (size=1)
      'Abuja' => string 'Abuja' (length=5)
  • 写回答

1条回答 默认 最新

  • donglu7816 2016-02-19 15:14
    关注

    Can you try:

    $locations = array_map(function(array $location){
                     return $location['Location'];
                 }, $this->repository->getLocationList());
    
    // and then in your form definition:
    
    'choice' => ['choices' => array_combine($locations, $locations)]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line