简体   繁体   English

原则2:OneToMany关系,未加载实体

[英]Doctrine 2 : OneToMany relation, Entity is not loaded

Here is my problem : I have the 3 entities Item, User and Link above (these classes also have the usual getters and setters). 这是我的问题:我上面有3个实体Item,User和Link(这些类也具有通常的getter和setter)。

class User {

    //...

    /*
     * @ORM\OneToMany(targetEntity="Link", mappedBy="user", cascade={"persist", "remove"})
     * 
     */
    protected $links;

    //...

}
class Item {

    //...

    /*
     * @ORM\OneToMany(targetEntity="Link", mappedBy="item", cascade={"persist", "remove"})
     * 
     */
    protected $links;

    //...

}
class Link {

    /**
     * @var datetime $time
     *
     * @ORM\Column(name="time", type="datetime")
     */
    private $time;

    /**
     * 
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="Item", inversedBy="links")
     * @ORM\JoinColumn(name="item_id", referencedColumnName="id")
     */
    private $item;

    /**
     *
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="User", inversedBy="links")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    private $user;

    //...

}

I did not use a ManyToMany relationship because of the $time property in the Link class. 由于Link类中的$ time属性,我没有使用ManyToMany关系。

When I create a Link, I do it this way : 当我创建一个链接时,我是这样做的:

$link = getExistingLink($item, $user);

if (!$link) {
    $link = new Link();
    $link->setItem($item);
    $link->setUser($user);
}
$link->setTime(new \DateTime());
$em = $this->getEntityManager();
$em->persist($link);
$em->flush();

The data is written in the database, however when I call $user->getLinks(), it returns NULL. 数据写在数据库中,但是当我调用$ user-> getLinks()时,它返回NULL。 I event tried to do this : 我事件试图做到这一点:

$user->addLink($link);
$em->persist($user);
$em->flush();

But the link won't be loaded the next time the $user will be loaded. 但是该链接将不会在下次加载$ user时加载。

Any idea why the Link entities are not loaded ? 知道为什么未加载Link实体吗?

OK Problem Solved. 确定问题已解决。

The annotations starts with /* instead of /** in both User and Item classes. 在用户和项目类中,注释均以/ *开头,而不是/ **开头。

Just a silly mistake ... 只是一个愚蠢的错误...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM