Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get doctrine repository in form type class in 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();

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

like image 275
Rajesh Vasani Avatar asked Mar 15 '12 05:03

Rajesh Vasani


1 Answers

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
}
like image 162
Jovan Perovic Avatar answered Nov 02 '22 20:11

Jovan Perovic