Exception with Doctrine2 and HYDRATE_SIMPLEOBJECT

2019-08-16 00:50发布

As a good practice i'm tring to hydrate an object as small as possible since data is going to be read only (just show the entity in my Twig template). So i've tried HYDRATE_SIMPLEOBJECT hydratation mode but i'm getting this exception:

Cannot use SimpleObjectHydrator with a ResultSetMapping that contains more than one object result.

How should i interpret this message? By the way, here is the code that throws the exception:

protected function getFindAllQueryBuilder()
{
    return $this->createQueryBuilder('p')
        ->select(array('p', 'parent', 'features', 'users'))
        ->leftJoin('p.parent', 'parent')
        ->leftJoin('p.features', 'features')
        ->leftJoin('p.users', 'users');
}

public function findOneBySlugAsObject($slug)
{
    $qb = $this->getFindAllQueryBuilder();

    return $qb
        ->where($qb->expr()->eq('p.slug', ':slug'))
        ->setParameter('slug', $slug)
            ->getQuery()->getOneOrNullResult(Query::HYDRATE_SIMPLEOBJECT);
}

1条回答
别忘想泡老子
2楼-- · 2019-08-16 01:06

SimpleObjectHydrator is for result sets where you don't use any fetch joins in your query, in other words you can't use it if you use more than one alias in your select. SimpleObjectHydrator is faster because doesn't handle these fetch joins.

查看更多
登录 后发表回答