Exception with Doctrine2 and HYDRATE_SIMPLEOBJECT

廉价感情. 提交于 2020-01-06 14:55:31

问题


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:


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.



来源:https://stackoverflow.com/questions/9745220/exception-with-doctrine2-and-hydrate-simpleobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!