how to get doctrine repository in form type class in symfony2?

南笙酒味 提交于 2019-12-01 03:24:28

问题


$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();

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


回答1:


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

This link:

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

Describes how to do it.

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...

Controller:

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

FormType:

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
}


来源:https://stackoverflow.com/questions/9714301/how-to-get-doctrine-repository-in-form-type-class-in-symfony2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!