Doctrine:在外部复合键中授权NULL [英] Doctrine: authorize NULL in a foreign composite key

查看:159
本文介绍了Doctrine:在外部复合键中授权NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下实体:

/**
 * SeriesAuthorRole
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Blog\Bundle\CoreBundle\Entity\SeriesAuthorRoleRepository")
 */
class SeriesAuthorRole extends AuthorRoleAbstract
{
    /**
     * @var Series
     *
     * @ORM\ManyToOne(targetEntity="Blog\Bundle\CoreBundle\Entity\Series", inversedBy="authors")
     * @ORM\JoinColumn(name="series", referencedColumnName="id", nullable=false)
     * @ORM\Id
     */
    private $series;

    /**
     * @var Author
     *
     * @ORM\ManyToOne(targetEntity="Blog\Bundle\CoreBundle\Entity\Author")
     * @ORM\JoinColumn(name="author", referencedColumnName="id", nullable=false)
     * @ORM\Id
     */
    protected $author;

    /**
     * @var Role
     *
     * @todo Must be nullable
     *
     * @ORM\ManyToOne(targetEntity="Blog\Bundle\CoreBundle\Entity\Role")
     * @ORM\JoinColumn(name="role", referencedColumnName="id", nullable=true)
     * @ORM\Id
     */
    protected $role;

    // ... Getters, setters
}

背后的想法很简单:我们有作者,作者和系列实体。一系列可以有几个作者有不同的角色。同一作者可以在一系列中完成多个角色。

The idea behind it is quite simple: We have author, role and series entities. A series can have several authors with various roles. A same author can fulfill multiple roles in a series.

有时,我们不知道作者的作用是什么。在这种情况下,NULL值将用于角色,NULL值代表我不知道。

Sometimes, we don't know exactly what was the role of the author. In this case, the NULL value will be used for the role, the NULL value standing for "I don't know".

我被教导不使用NULL外来复合键除非有意义。那么这里有含义,我知道这可以在没有 Doctrine 的情况下实现。但是,现在,Symfony 2抛出错误:

I was taught not to use NULL in foreign composite keys unless it has meaning. Well, it has meaning here, and I know that this could be implemented without Doctrine. However, for now, Symfony 2 throws that error:

Entity of type Blog\Bundle\CoreBundle\Entity\BandAuthorRole is missing an assigned ID for field 'role'. The identifier generation strategy for this entity requires the ID field to be populated before EntityManager#persist() is called. If you want automatically generated identifiers instead you need to adjust the metadata mapping accordingly.
500 Internal Server Error - ORMException

那么如何在外部复合键中授权NULL值?

So how can I authorize NULL values in foreign composite keys ? Is it possible at all with Doctrine ?

推荐答案

您的@JoinColumn注释是正确的,参考< a href =http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#annref-joincolumn =nofollow> http:// docs。 doctrine-project.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#annref-joincolumn

Your @JoinColumn annotation is correct with referencing to http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#annref-joincolumn

但是, p>

However,


具有复合键的每个实体不能使用其他
的ID生成器ASSIGNED。这意味着ID字段必须在调用EntityManager#persist($ entity)之前设置它们的值

Every entity with a composite key cannot use an id generator other than "ASSIGNED". That means the ID fields have to have their values set before you call EntityManager#persist($entity).

< a href =http://docs.doctrine-project.org/en/2.0.x/tutorials/composite-primary-keys.html#general-considerations =nofollow> http://docs.doctrine- project.org/en/2.0.x/tutorials/composite-primary-keys.html#general-considerations

这篇关于Doctrine:在外部复合键中授权NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆