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

时间:2016-07-17 20:42:48

标签: 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();
    }
}

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);
    }
}

其中每个都有一个save()功能的经理类,只使用persist()flush()。然后保存它们:

$pageManager->save($post);
$url = new Url($post, 'link goes here');
$urlManager->save($url);

我也试过了:

$url = new Url($post, 'link goes here');
$pageManager->save($post);
$urlManager->save($url);

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

(!)致命错误:未捕获的异常'Doctrine \ ORM \ ORMInvalidArgumentException',消息'无法安排托管+脏实体页面@ 000000003d5a4ca10000000133ba3c3e插入。'

我在AnnotationReader使用EntityManager::create()时使用和不使用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.

Doctrine的架构验证器也不报告任何错误:

persist()

如何让template< class GRAPH_t, class NODE_REGISTRAR, class EDGE_REGISTRAR, class OPTIMIZER > class CGraphSlamEngine { public: ... }; 工作?

2 个答案:

答案 0 :(得分:0)

想通了:

我不得不颠倒他们保存的顺序,因此首先保存Url,然后保存Page(尽管这样做会打开我需要解决的另一个问题它想要坚持作者实体,但不能(它认为它是新的,但它不是)。想想也许它是分离的,但即使是merge()也没有&#39解决它。

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

答案 1 :(得分:0)

您必须在刷新之前保留$ post和$ url对象。否则您将收到此错误消息。

尝试这样做......

$entityManager->persist($post);
$url = new Url($post, 'link goes here');
$entityManager->persist($url);
$entityManager->flush();

或者您可以在经理上创建一个标志参数,如果错误则不刷新...

$flush = false;
$pageManager->save($post, $flush);
$url = new Url($post, 'link goes here');
$urlManager->save($url);

它可能在没有任何额外资源的情况下工作。