/** @Entity */
class First
{
/** @OneToMany(targetEntity="Second", mappedBy="parent") */
protected $secondList;
// access methods here
public function __construct()
{
$this->secondList = new ArrayCollection();
}
}
/** @Entity */
class Second
{
/**
* @ManyToOne(targetEntity="First", inversedBy="secondList")
* @JoinColumn(name="First_id", referencedColumnName="Id")
*/
protected $parent;
}
这是从类中获取ArrayCollection $secondList
元素的问题。多对一关系工作正常。也许我在初始化持久性时做错了(因为在 SQL 基础中总是如此)。Second
Second
First_Id
null
$first = new Second();
$second = new First();
$first->getSecond()->add($second);
$em->persist($second);
$em->persist($first);
有什么建议么?