问题
$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