如何在symfony2的表单类型类中获取学说存储库? [英] how to get doctrine repository in form type class in symfony2?

查看:20
本文介绍了如何在symfony2的表单类型类中获取学说存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$repository = $this->getDoctrine()->getRepository('ParabolaEntityBundle:ProjectAllocation');
        $query = $repository->createQueryBuilder('p')
                ->where('p.startDate < :sdate and p.employee = :emp and p.endDate > :edate')
                ->setParameter('sdate', date('Y-m-d', time()))
                ->setParameter('edate', date('Y-m-d', time()))
                ->setParameter('emp', $employee->getId())
                ->getQuery();
        $projectAllocate = $query->getResult();

我如何在 FormType 类中使用上述代码.我正在使用此查询为表单生成器中的选择类型生成数组.

how can i use above code in FormType class.I am using this query to generate array for the choice type in form builder.

推荐答案

我认为你应该使用 entity 类型代替它有一个 query_builder 选项.

I think you should use entity type instead which has a query_builder option.

此链接:

http://symfony.com/doc/current/reference/forms/types/entity.html

描述如何去做.

如果由于某种原因你真的不想使用 entity 类型,你总是可以在控制器中检索数据并通过 FormType 构造函数传递它,这是一个有点快'n'脏,但应该可以正常工作......

If, for some reason you really don't want to use entity type, you could always retrieve data within controller and pass it via FormType constructor, which is a little bit quick 'n' dirty but should work just fine...

控制器:

$this->createForm(new MyFormType($results_from_qb), $form_data );

表单类型:

public function __construct($results_from_qb){
    $this->results_from_qb = $results_from_qb; // store it into class member field to be used latter in buildForm method
}

这篇关于如何在symfony2的表单类型类中获取学说存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆