Mick
Mick

Reputation: 31919

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

}

Upvotes: 10

Views: 6677

Answers (1)

l3l0
l3l0

Reputation: 3393

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

Upvotes: 15

Related Questions