Doctrine query using execute
Option 1
and Option 2
seem to give similar results. Is there a specific benefit to using over the execute statement
usual method getResult()
?
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();
}
source to share
Basically getResult()
is an alias for execute(array())
which you can set as argument hydration mode, e.g .: getResult(Query::HYDRATE_OBJECT)
isexecute(array(), Query::HYDRTE_OBJECT)
The only difference is, in the execute method, you can set query parameters as the first argument, so you don't need to call the method setParameter
before ...
source to share