Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine query using execute

Option 1 and Option 2 seem to give similar results. Is there a particular advantage for using the execute statement rather than the usual getResult() method?

Option 1:

public function getEventsByOrganiser(EventInterface $event, $username)
{
    $qb = $this->repository->createQueryBuilder('e')
        ->select(array('e', 'u'))
        ->leftJoin('e.user', 'u')
        ->andWhere('u.username = :username');

    return $qb->getQuery()->execute(array(
        'username' => $username
    ));
}

Option2:

public function getEventsByOrganiser(EventInterface $event, $username)
{
    $qb = $this->repository->createQueryBuilder('e')
        ->select(array('e', 'u'))
        ->leftJoin('e.user', 'u')
        ->andWhere('u.username = :username')
        ->setParameter('username', $username);

    return $qb->getQuery()->getResult();

}
like image 815
Mick Avatar asked Jan 31 '13 06:01

Mick


1 Answers

Basically getResult() is alias for execute(array()) you can set as argument hydration mode for example: getResult(Query::HYDRATE_OBJECT) is execute(array(), Query::HYDRTE_OBJECT)

Only difference: in execute method you can set query parameters as first argument so you do not have to call setParameter method before...

like image 57
l3l0 Avatar answered Oct 10 '22 20:10

l3l0