Here is my problem : I have the 3 entities Item, User and Link above (these classes also have the usual getters and setters).
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.
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. 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.
Any idea why the Link entities are not loaded ?
OK Problem Solved.
The annotations starts with /* instead of /** in both User and Item classes.
Just a silly mistake ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.