Symfony 2:getReference并找到 [英] Symfony 2 : getReference and find

查看:129
本文介绍了Symfony 2:getReference并找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用实体管理器 getReference() find()方法返回一个未初始化的对象,数据库。你知道为什么和应该做什么?

Using the entity manager getReference() or find() method returns a non initialized object for some records of the database. Do you know why and what should be done?

推荐答案

getReference()如果尚未加载对象,则不会加载对象,它只会将对象返回给对象。

getReference() does not load the object if it has not been loaded yet, it only returns a proxy to the object.

find()返回一个加载对象。

find() returns a loaded object.

cfr 文档

// this call does not trigger a db query, but creates an empty proxy with the ID
$objectA = $this->entityManager->getReference('EntityName', 1);

$this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $objectA); // === true

// this will trigger a query, loading the state that's configured to eager load
// since the UnitOfWork already has a proxy, that proxy will be reused
$objectB = $this->entityManager->find('EntityName', 1);

$this->assertSame($objectA, $objectB); // === true

getReference()存在用于特殊用例的情况下,如果要获取对象以使用它们,请始终使用 find()

getReference() exists for special use cases, if you are fetching objects to use them, always use find().

这篇关于Symfony 2:getReference并找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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