如何从DQL查询返回对象? [英] How to return object from a DQL query?

查看:82
本文介绍了如何从DQL查询返回对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Doctrine 2中写了一个DQL查询:

  $ qb-> select('r.position') 
- > from('\Entities\Races','r')
- > where($ qb-> expr() - > eq('r.entrantId' ':entrant_id'))
- > setParameter('entrant_id',$ this-> entrantId);
$ query = $ qb-> getQuery();
$ aRaces = $ query-> getResult();

目前,它在数组中返回查询结果,如下所示:

 数组

[0] =>数组

[position] => 10


[1] =>数组

[position] => 4


我希望结果返回一个Races 对象数组,以便我可以访问方法与对象关联(我很确定默认情况下以前版本的Doctrine返回对象)。



我试过:

  $ aRaces = $ query-> getResult(Query :: HYDRATE_OBJECT); 

但这没有什么不同。



欣赏帮助

解决方案

您只能获取位置从DB。尝试用 select(r)替换 select('r.position')。请参阅 DQL参考



如果您只需要 position 属性的对象,请参阅部分对象


I have written a DQL query in Doctrine 2:

$qb->select('r.position')
    ->from('\Entities\Races', 'r')
    ->where($qb->expr()->eq('r.entrantId', ':entrant_id'))
    ->setParameter('entrant_id', $this->entrantId);
$query = $qb->getQuery();
$aRaces = $query->getResult();

Currently it returns the results of the query in an array like so:

Array
(
    [0] => Array
        (
            [position] => 10
        )

    [1] => Array
        (
            [position] => 4
        )
)

I want the result to return an array of Races objects so that I can access the methods associated with the object (I'm pretty sure the previous version of Doctrine returned objects by default).

I tried:

$aRaces = $query->getResult(Query::HYDRATE_OBJECT);

But that didn't make a difference.

Appreciate the help

解决方案

You are fetching only position column from DB. Try replacing select('r.position') with select(r). See DQL reference

If you need objects with only position attribute, refer to partial objects

这篇关于如何从DQL查询返回对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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