doulianxing4015 2014-07-03 09:40
浏览 34
已采纳

在symfony2 QueryBuilder中使用$ this

I have the following code.

 $form = $this->createFormBuilder()
                ->add('distributor', 'entity', array(
                    'class' => 'AdminBundle:Customers',
                    'query_builder' => function(EntityRepository $repository) {
                        return $repository->createQueryBuilder('c')
                                ->where('c.customerType =:type')
                                ->Andwhere('c.status =:status')
                                ->andWhere('c.district =:district')
                                ->setparameter('status', '1')
                                ->setparameter('type', '1')
                                ->setparameter('district', $this->get("security.context")->getToken()->getUser()->getCustomer()->getDistrict()->getId())
                        ;
                    },
                    'property' => 'customerName',
                    'empty_value' => 'Select Distributor',
                    'multiple' => FALSE,
                    'expanded' => FALSE,
                    'required' => TRUE,
                        )
                )
                ->add('excel_file', 'file'
                ->getForm();

When I am going to use distributor element in the twig like this

 {{ form_widget(form.distributor,{ 'attr': {'class': 'input-box'} }) }}   

it is getting an error... Fatal error: Using $this when not in object context

How to use $this in the symfony QueryBuilder with form?

Thanks Rakhitha

  • 写回答

1条回答 默认 最新

  • dsjws44266 2014-07-03 09:48
    关注

    You are probably using PHP 5.3, and thus you cannot use $this in a closure. To fix this issue, do the following:

     $id = $this->get("security.context")->getToken()->getUser()->getCustomer()->getDistrict()->getId();
     $form = $this->createFormBuilder()
                    ->add('distributor', 'entity', array(
                        'class' => 'AdminBundle:Customers',
                        'query_builder' => function(EntityRepository $repository) use ($id) {
                            return $repository->createQueryBuilder('c')
                                    ->where('c.customerType =:type')
                                    ->Andwhere('c.status =:status')
                                    ->andWhere('c.district =:district')
                                    ->setparameter('status', '1')
                                    ->setparameter('type', '1')
                                    ->setparameter('district', $id)
                            ;
                        },
                        'property' => 'customerName',
                        'empty_value' => 'Select Distributor',
                        'multiple' => FALSE,
                        'expanded' => FALSE,
                        'required' => TRUE,
                            )
                    )
                    ->add('excel_file', 'file'
                    ->getForm();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥50 74LS系列 74LS00 74LS04设计一个RS485电路(关键词-差分)
    • ¥30 各位help写一下代码
    • ¥15 在运行SDEdit模型下载不了
    • ¥15 求51控制l298n驱动的小车中超声波避障怎么写
    • ¥15 电脑连上WIFI却用不了
    • ¥30 MATLAB在RLC电路的固有响应和阶跃响应GUI仿真报告
    • ¥15 hyper-v出现的问题
    • ¥15 有能用的可加酬金,求可以批量下载懒人听书的软件,能登录自己帐号的。
    • ¥100 高博一起做RGB-D SLAM(5)VO无法出visualisation问题
    • ¥15 使用matlab进行手眼标定的仿真验证,得到齐次矩阵与opencv相差较大