与@Embedded列的关系

时间:2018-02-25 19:42:27

标签: symfony doctrine-orm

我有@Embeddable类ID:

/**
 * @ORM\Embeddable()
 */
final class Id
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="string")
     */
    private $id;

    //getters, setters, etc
}

两个有@ManyToOne关系的类:

/**
 * @ORM\Entity()
 */
final class Gist
{
    /**
     * @var Id
     *
     * @ORM\Id()
     * @ORM\Embedded(class="App\Entity\Id")
     */
    private $id;

    /**
     * @var User
     *
     * @ORM\ManyToOne(targetEntity="App\Entity\User")
     */
    private $user;

    //getters, setters, etc
}

User课程:

/**
 * @ORM\Entity()
 */
final class User implements UserInterface
{
    /**
     * @var Id
     *
     * @ORM\Embedded(class="App\Entity\Id")
     *
     */
    private $id;

    //getters, setters, etc

}

当我尝试创建架构时,我收到错误:No mapping found for field 'id' on class 'App\Entity\User'.。我做错了什么?

2 个答案:

答案 0 :(得分:0)

我建议不要在id字段上使用@Embeddable。因为它不会工作。正常声明你的id字段,它应该可以工作。

/**
 * @ORM\Entity()
 */
class Gist
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @var User
     *
     * @ORM\ManyToOne(targetEntity="App\Entity\User")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    private $user;

    //getters, setters, etc
}

/**
 * @ORM\Entity()
 */
class User implements UserInterface
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    //getters, setters, etc

}

您的多对一关系也很糟糕,请参阅:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-one-unidirectional

永远不要将实体声明为最终类,请参阅:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/architecture.html#entities

答案 1 :(得分:0)

没有解决方案。 https://github.com/doctrine/doctrine2/issues/7094

  

支持@Embeddable中的标识符还不存在,抱歉。