10

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

}

1 Answer 1

15

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

Sign up to request clarification or add additional context in comments.

1 Comment

Just citing the doc for people to look into if they need to: query-result-formats

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.