Doctrine orm 理论中的自指实体

Doctrine orm 理论中的自指实体,doctrine-orm,doctrine,Doctrine Orm,Doctrine,我的实体中需要一个树结构。 我的要求是引用父实体和根实体。 根实体也应该有一个对自身的引用。这就产生了问题。 除其他事项外,我的实体还包括以下字段: /** * @ORM\Id() * @ORM\Column(type="uuid", unique=true) * @ORM\GeneratedValue(strategy="CUSTOM") * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctr

我的实体中需要一个树结构。 我的要求是引用父实体和根实体。 根实体也应该有一个对自身的引用。这就产生了问题。 除其他事项外,我的实体还包括以下字段:

/**
 * @ORM\Id()
 * @ORM\Column(type="uuid", unique=true)
 * @ORM\GeneratedValue(strategy="CUSTOM")
 * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
 */
private UuidInterface $id;

/**
* @ORM\ManyToOne(targetEntity="App\Entity\Example")
* @ORM\JoinColumn(nullable=false)
*/
private self $root;
然后在persist和flush之前填充根引用

$example->setRoot($example);

$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($example);
$entityManager->flush();
但这会导致以下PostgreSQL错误,因为根实体未被很好地引用:

使用参数[“d1ed4480-e872-4c13-bbd8-7597d465a3cd”,null,null]:

SQLSTATE[23502]:非空冲突:7错误:“root\u id”列中的null值违反非空约束 详细信息:失败的行包含(d1ed4480-e872-4c13-bbd8-7597d465a3cd,null,null)

首先保存实体并在之后修改它是很容易的。但是因为我已经生成了主键,所以不需要绕道