doctrine2关联未初始化 [英] doctrine2 association is not initialized

查看:105
本文介绍了doctrine2关联未初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类如下:

  / ** @Entity ** / 
class orgGroup {

// id和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的所有者,它只是一个对象而不是一个数组。我的Web服务总是只是说:

  {error:[],warning:[],message ],data:[{name:AdministratorGroup,description:null,_ orgGroupType:{__ isInitialized __:false}}]} 
/ pre>

结果是:

 name管理员组,
description:null,
_orgGroupType:{__ isInitialized __:false}

但应该是:

 name:AdministratorGroup,
描述:一些描述,
_orgGroupType:{name:test}

所以有 2 错误...我不知道为什么。
所有的数据都在数据库中正确设置。



任何想法?



编辑:
这是我的orgGroupType -entity缺少的代码

  / ** @Entity ** / 
class orgGroupType {
/ **
* @OneToMany(targetEntity =orgGroup,mappedBy =_ orgGroupType)
** /
private $ _orgGroups;

public function __construct()
{
$ this-> _orgGroups = new ArrayCollection();
}
}


解决方案

看起来像一个懒惰装载的问题。如何从对象中获取数据到Webservice答案?



如果您没有配置其他内容,Doctrine2将被延迟加载,这意味着您的 $ groups = $ em-> getRepository(orgGroup) - > findAll(); 不会返回真实的 orgGroup ,但Proxy对象(教义文档)。



这意味着一个 $ group 对象不会有其描述或orgGroupType值直到你调用 $ group-> getDescription() $ group-> getOrgGroupType()(然后Doctrine加载它们自动),因此您需要在将数据写入web服务的JSON响应之前执行此操作。如果您以某种方式循环访问对象属性而不使用getter方法,它将无法正常工作。



我希望这是问题:)


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;

    //...
}

But when i load this Object from my database via

$groups = $em->getRepository("orgGroup")->findAll();

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}}]}

The result is:

"name":"AdministratorGroup",
"description":null,
"_orgGroupType":{"__ isInitialized __":false}

but should be:

"name":"AdministratorGroup",
"description":"some description",
"_orgGroupType":{name:"test"}

So there are 2 errors... and I have no idea why. All the data is set correctly in the database.

Any Ideas?

EDIT: 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();
    }
}

解决方案

This looks to me like a lazy-loading-issue. How do you get the data from the object into the Webservice answer?

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).

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 :)

这篇关于doctrine2关联未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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