学说2关联未初始化
我有一个如下所示的类:
I have a Class like the following:
/** @Entity **/
class orgGroup{
//id and stuff...
/**
* @Column(type="string")
**/
private $name;
/**
* @Column(type="string", nullable=true)
**/
private $description;
/**
* @ManyToOne(targetEntity="orgGroupType", inversedBy="_orgGroups")
* @JoinColumn(name="_orgGroupType")
**/
private $_orgGroupType;
//...
}
但是当我通过
$groups = $em->getRepository("orgGroup")->findAll();
我只是正确地得到了名称,但不是 _orgGroupType...我不知道为什么... OrgGroup 是 orgGroupType 的所有者,它只是一个对象而不是数组.我的网络服务总是说:
I just get the name correctly but not the _orgGroupType... and i dont know why... OrgGroup is the owner of orgGroupType and its just ONE object and not an array. My Webservice always just says:
{"error":[],"warning":[],"message":[],"data":[{"name":"AdministratorGroup","description":null,"_orgGroupType":{"__ isInitialized __":false}}]}
结果是:
"name":"AdministratorGroup",
"description":null,
"_orgGroupType":{"__ isInitialized __":false}
但应该是:
"name":"AdministratorGroup",
"description":"some description",
"_orgGroupType":{name:"test"}
所以有 2 个错误......我不知道为什么.数据库中的所有数据都设置正确.
So there are 2 errors... and I have no idea why. All the data is set correctly in the database.
有什么想法吗?
这是我的 orgGroupType -entity 的缺失代码
Here's the missing code of my orgGroupType -entity
/** @Entity **/
class orgGroupType {
/**
* @OneToMany(targetEntity="orgGroup", mappedBy="_orgGroupType")
**/
private $_orgGroups;
public function __construct()
{
$this->_orgGroups = new ArrayCollection();
}
}
推荐答案
这在我看来就像一个延迟加载问题.如何将对象的数据获取到 Webservice 答案中?
This looks to me like a lazy-loading-issue. How do you get the data from the object into the Webservice answer?
Doctrine2 是延迟加载,如果你不配置其他东西,这意味着你的 $groups = $em->getRepository("orgGroup")->findAll();
不会't 返回真正的 orgGroup
对象,但 Proxy 对象(教义文档).
Doctrine2 is lazy-loading if you don't configure something else, that means your $groups = $em->getRepository("orgGroup")->findAll();
won't return real orgGroup
objects, but Proxy objects (Doctrine Documentation).
这意味着 $group
对象不会有它的 description 或 orgGroupType 值,直到您调用 $group->getDescription()
或 $group->getOrgGroupType()
(然后 Doctrine 会自动加载它们),因此您需要在将数据写入 Web 服务的 JSON 响应之前执行此操作.如果您以某种方式循环遍历对象属性而不使用 getter 方法,它将无法工作.
That means a $group
object won't have it's description or orgGroupType value until you call $group->getDescription()
or $group->getOrgGroupType()
(then Doctrine loads them automatically), so you need to do that before writing the data into the JSON-response for the webservice. It won't work if you somehow loop through the object properties without using the getter methods.
我希望这就是问题所在:)
I hope that was the problem :)
相关文章