php 学说2关联未初始化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13431614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:23:56  来源:igfitidea点击:

doctrine2 association is not initialized

phpsymfonydoctrinedoctrine-orm

提问by Laokoon

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:

我只是正确地得到了名称,但没有得到 _orgGroupType ......我不知道为什么...... OrgGroup 是 orgGroupType 的所有者,它只是一个对象而不是数组。我的网络服务总是说:

{"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 2errors... and I have no idea why. All the data is set correctly in the database.

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

Any Ideas?

有任何想法吗?

EDIT: Here's the missing code of my orgGroupType -entity

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

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

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

回答by Praveen

Try to change fetch mode to EAGER.

尝试将 fetch 模式更改为EAGER

@ORM\ManyToOne(targetEntity="****", fetch="EAGER"). 

It worked for me.

它对我有用。

回答by Manuel

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

这在我看来像是一个延迟加载问题。你如何从对象中获取数据到 Webservice 答案中?

Doctrine2 is lazy-loading if you don't configure something else, that means your $groups = $em->getRepository("orgGroup")->findAll();won't return real orgGroupobjects, but Proxy objects (Doctrine Documentation).

如果您不配置其他内容,Doctrine2 是延迟加载的,这意味着您$groups = $em->getRepository("orgGroup")->findAll();不会返回真实orgGroup对象,而是返回代理对象(Doctrine 文档)。

That means a $groupobject 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.

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

I hope that was the problem :)

我希望这是问题:)

回答by TNP

//Group.php ...

public function addUser(User $user): self
{
    if (!$this->users->contains($user)) {
        $this->users[] = $user;
        $user->addJoinedGroup($this);     /** VERY IMPORTANT **/
    }
    return $this;
}

Same in User.phpWithout it, it didn't do anything in my database

同样User.php没有它,它在我的数据库中没有做任何事情

回答by Pankrates

Be sure to initialize the orgGroups collection in the orgGroupType entity

务必在 orgGroupType 实体中初始化 orgGroups 集合

/**
 * @OneToMany(targetEntity="orgGroup", mappedBy="_orgGroupType")
 */
protected $orgGroups ;

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

You might need to include the following in the Entity

您可能需要在实体中包含以下内容

use Doctrine\Common\Collections\Collection,
Doctrine\Common\Collections\ArrayCollection;