Symfony 3 ManyToOne关系空相关集合
Symfony 3 ManyToOne relationship empty related Collection
使用 Symfony 3.4,我正在保存一个具有 ManyToOne 关系的实体。在数据库中,关系被正确保存,但在实体中,相关的 属性 仍然是一个空集合,而不是用相关实体填充。我需要这个,所以我可以 return 一个完整的 JSON 对象作为 API 响应。完整意味着还包含相关属性。
我也试过 fetch=EAGER
但它并没有改变结果。
User.php
/**
* @ORM\OneToMany(targetEntity="PGS", mappedBy="user", fetch="EAGER")
*/
private $pgs;
public function __construct()
{
$this->pgs = new ArrayCollection();
}
PGS.php
/**
* @ORM\ManyToOne(targetEntity="G", fetch="EAGER")
* @ORM\JoinColumn(name="gId", referencedColumnName="id")
* @ORM\Id
*
*/
private $g;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="pgs", fetch="EAGER")
* @ORM\JoinColumn(name="userId", referencedColumnName="id")
* @ORM\Id
*
*/
private $user;
/**
* @ORM\ManyToOne(targetEntity="S", fetch="EAGER")
* @ORM\JoinColumn(name="sId", referencedColumnName="id")
* @ORM\Id
*
*/
private $s;
/**
* @ORM\ManyToOne(targetEntity="P", fetch="EAGER")
* @ORM\JoinColumn(name="pId", referencedColumnName="id")
* @ORM\Id
*
*/
private $p;
为了简化代码,假设 3 个实体 P、G 和 S 具有相应的 OneToMany
目标 PGS(无论如何它们可能与此问题无关)。
UserManager.php
// $user is a valid User entity constructed before. I want to store it.
$this->entityManager->persist($user);
foreach ($p in $arrayOfP) {
// $g and $s are known and valid
$pgs = new PGS();
$pgs->setUser($user);
$pgs->setG($g);
$pgs->setS($s);
$pgs->setP($p);
$this->entityManager->persist($pgs);
}
$this->entityManager->flush();
// At this point I would expect the $user object to contain the pgs
// Instead I get an empty collection.
dump(count($user->getPGS())) // returns 0
// in the DB both User and PGS are properly created
我错过了什么?
你在 setUser 方法中的 PGS 实体中有这样的东西吗
public function setUser(UserInterface $user){
$user->addPgs($this);
$this->user = $user;
}
我认为你必须添加,然后如果你转储你应该有一个集合。
使用 Symfony 3.4,我正在保存一个具有 ManyToOne 关系的实体。在数据库中,关系被正确保存,但在实体中,相关的 属性 仍然是一个空集合,而不是用相关实体填充。我需要这个,所以我可以 return 一个完整的 JSON 对象作为 API 响应。完整意味着还包含相关属性。
我也试过 fetch=EAGER
但它并没有改变结果。
User.php
/**
* @ORM\OneToMany(targetEntity="PGS", mappedBy="user", fetch="EAGER")
*/
private $pgs;
public function __construct()
{
$this->pgs = new ArrayCollection();
}
PGS.php
/**
* @ORM\ManyToOne(targetEntity="G", fetch="EAGER")
* @ORM\JoinColumn(name="gId", referencedColumnName="id")
* @ORM\Id
*
*/
private $g;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="pgs", fetch="EAGER")
* @ORM\JoinColumn(name="userId", referencedColumnName="id")
* @ORM\Id
*
*/
private $user;
/**
* @ORM\ManyToOne(targetEntity="S", fetch="EAGER")
* @ORM\JoinColumn(name="sId", referencedColumnName="id")
* @ORM\Id
*
*/
private $s;
/**
* @ORM\ManyToOne(targetEntity="P", fetch="EAGER")
* @ORM\JoinColumn(name="pId", referencedColumnName="id")
* @ORM\Id
*
*/
private $p;
为了简化代码,假设 3 个实体 P、G 和 S 具有相应的 OneToMany
目标 PGS(无论如何它们可能与此问题无关)。
UserManager.php
// $user is a valid User entity constructed before. I want to store it.
$this->entityManager->persist($user);
foreach ($p in $arrayOfP) {
// $g and $s are known and valid
$pgs = new PGS();
$pgs->setUser($user);
$pgs->setG($g);
$pgs->setS($s);
$pgs->setP($p);
$this->entityManager->persist($pgs);
}
$this->entityManager->flush();
// At this point I would expect the $user object to contain the pgs
// Instead I get an empty collection.
dump(count($user->getPGS())) // returns 0
// in the DB both User and PGS are properly created
我错过了什么?
你在 setUser 方法中的 PGS 实体中有这样的东西吗
public function setUser(UserInterface $user){
$user->addPgs($this);
$this->user = $user;
}
我认为你必须添加,然后如果你转储你应该有一个集合。