如何在Doctrine 2中获取类而不是数组 [英] How to fetch class instead of array in Doctrine 2

查看:72
本文介绍了如何在Doctrine 2中获取类而不是数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用以下结构从数据库中获取数据:

  $ user = $ this-> getDoctrine ()
- > getRepository('AcmeDemoBundle:Emails')
- > find(8081);

当我这样做,我可以得到我的数据:

  $ user-> getColumnNameHere(); 

基本上我可以使用实体类。



但是,如果我想使用QueryBuilder而不是 find 我只能获得关联数组。

  $ product-> createQueryBuilder('p')
- > setMaxResults(1)
- > where('p.idx = :idx')
- > select('p.columnNameHere')
- > setParameter('idx',8081)
- > orderBy('p.idx' DESC')
- > getQuery();
$ product = $ query-> getResult();

$ product returnds as array。是否可以通过实体Managaer类获取它?如果是,如何?



我挖掘文档,但在文档中似乎不可能或不存在,或者我只是盲目:)

$ b $你可以通常使用

  $存储库
- > createQueryBuilder('p')
- > getQuery()
- > execute()
;

这应该会返回一个实体数组。



如果要获取单个实体结果,请使用 getSingleResult getOneOrNullResult

  $ repository 
- > createQueryBuilder('p')
- > getQuery()
- > ; getOneOrNullResult()
;

警告:这些方法可能会抛出 NonUniqueResultException



编辑:好的,所以问题是关于部分对象: http://docs.doctrine-project.org/en/latest/reference/partial-objects.html


I am able to fetch my data from database by using this structure:

$user = $this->getDoctrine()
->getRepository('AcmeDemoBundle:Emails')
->find(8081);

When I do that, I am able to get my data like this:

$user->getColumnNameHere();

Basically I am able to use Entity Class.

But if I want to use QueryBuilder instead of find I am only getting associative arrays.

$product->createQueryBuilder('p')
        ->setMaxResults(1)
        ->where('p.idx = :idx')
        ->select('p.columnNameHere')
        ->setParameter('idx', 8081)
        ->orderBy('p.idx', 'DESC')
        ->getQuery();
        $product = $query->getResult();

$product returnds as array. Is it possible to fetch it withj Entity Managaer Class? If yes, how?

I digg the documentation but it seems not possible or not exist in the doc or I'm just blind :)

解决方案

Yes you can, usually using:

$repository
    ->createQueryBuilder('p')
    ->getQuery()
    ->execute()
;

This should return you an array of entities.

If you want to get a single entity result, use either getSingleResult or getOneOrNullResult:

$repository
    ->createQueryBuilder('p')
    ->getQuery()
    ->getOneOrNullResult()
;

Warning: These method can potentially throw NonUniqueResultException.

Edit: Ok, so the question was about partial objects: http://docs.doctrine-project.org/en/latest/reference/partial-objects.html

这篇关于如何在Doctrine 2中获取类而不是数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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