Doctrine2 ORM - 无法安排插入的托管+脏实体 X

jed*_*i58 6 php orm doctrine-orm

我有以下课程(仅显示部分以减少阅读量)

class Page {
    /**
     * @ORM\Column(type="string", unique=true, nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="UUID")
     * @var string
     */
    protected $id;
    /**
     * @ORM\OneToMany(targetEntity="Url", mappedBy="content")
     * @var Url[]
     */
    protected $urls;

    public function __construct()
    {
        $this->urls = new ArrayCollection();
    }
}
Run Code Online (Sandbox Code Playgroud)

和:

class Url
{
    /**
     * @ORM\Id @ORM\Column(type="string", unique=true, nullable=false)
     * @ORM\GeneratedValue(strategy="UUID")
     * @var string The unique identifier for the Url
     */
    protected $id;
    /**
     * @ORM\ManyToOne(targetEntity="Page", inversedBy="urls", cascade={"persist", "merge"})
     * @ORM\JoinColumn(name="content_id", referencedColumnName="id")
     * @var int The UUID of the content
     */
    protected $content;

    public function __construct(Page $content, $link)
    {
        $this->content = $content;
        $this->content->addUrl($this);
    }
}
Run Code Online (Sandbox Code Playgroud)

其中每一个都有一个管理器类,save()其中包含一个只使用persist()and的函数flush()。然后将它们保存为:

$pageManager->save($post);
$url = new Url($post, 'link goes here');
$urlManager->save($url);
Run Code Online (Sandbox Code Playgroud)

我也试过:

$url = new Url($post, 'link goes here');
$pageManager->save($post);
$urlManager->save($url);
Run Code Online (Sandbox Code Playgroud)

虽然在这两种情况下我都得到:

( ! ) 致命错误:未捕获的异常“Doctrine\ORM\ORMInvalidArgumentException”,消息为“A managed+dirty entity Page@000000003d5a4ca10000000133ba3c3e 无法安排插入”。

我既没有尝试使用这个AnnotationReader正与使用EntityManager::create()

Doctrine 的模式验证器也不报告任何错误:

php vendor/bin/doctrine orm:validate-schema
[Mapping]  OK - The mapping files are correct.
[Database] OK - The database schema is in sync with the mapping files.
Run Code Online (Sandbox Code Playgroud)

任何想法如何让persist()工作?

jed*_*i58 0

弄清楚了:

我必须颠倒它们保存的顺序,因此Url首先保存,然后Page(尽管这样做会引发另一个问题,我需要解决它想要保留作者实体但不能的地方(它认为这是新的) ,但事实并非如此)。以为可能是分离的,但即使是也merge()不能解决问题。

/**
 * @ORM\ManyToOne(targetEntity="User")
 * @ORM\JoinColumn(name="author_id", referencedColumnName="id")
 * @var string The UUID of the author for the {@link Page}
 */
protected $author;
Run Code Online (Sandbox Code Playgroud)