Doctrine2和HYDRATE_SIMPLEOBJECT的异常 [英] Exception with Doctrine2 and HYDRATE_SIMPLEOBJECT

查看:64
本文介绍了Doctrine2和HYDRATE_SIMPLEOBJECT的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个好习惯,我打算尽可能缩小对象,因为数据将只读(只需在我的Twig模板中显示实体)。因此,我尝试了 HYDRATE_SIMPLEOBJECT 的水合作用模式,但是我遇到了以下异常情况:

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:


不能将SimpleObjectHydrator与一个包含多个对象结果的
的ResultSetMapping一起使用。

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


推荐答案

SimpleObjectHydrator用于结果集,其中您不会在查询中使用任何提取联接,换句话说,如果您在选择中使用多个别名,则无法使用它。 SimpleObjectHydrator更快,因为不处理这些提取联接。

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.

这篇关于Doctrine2和HYDRATE_SIMPLEOBJECT的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆