10

Option 1并且Option 2似乎给出了类似的结果。使用execute statement而不是通常的getResult()方法有什么特别的优势吗?

选项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
    ));
}

选项2:

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

}
4

1 回答 1

15

基本上getResult()是别名,execute(array())您可以将其设置为参数水合模式,例如:getResult(Query::HYDRATE_OBJECT)execute(array(), Query::HYDRTE_OBJECT)

唯一的区别:在执行方法中,您可以将查询参数设置为第一个参数,因此您不必setParameter在之前调用方法...

于 2013-01-31T07:13:50.913 回答