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)]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系