主键和外键同时存在的原则2

发布于 2024-11-16 01:17:25 字数 1023 浏览 1 评论 0原文

我有两个表:

Aid 作为主键

Bid 作为主键和外键key

简短说明:

我需要在表 B 中有一个主键,它也是指向表 A 的主键的外键。

谁能解释一下如何通过 Doctrine 2 中的注释来映射它?

注意:

我尝试过这样:

   class A
{
    /**
     * @var bigint $id
     *
     * @Column(name="id", type="bigint", nullable=false)
     * @Id
     * @GeneratedValue(strategy="IDENTITY")
     */
    private $a_id;
...

B 表:


class B
{
    /**
     * @var bigint $id
     * @Id 
     * @OneToOne(targetEntity="A", fetch="LAZY")
     * @JoinColumn(name="id", referencedColumnName="id")
     */
    private $b_id;
...

但它给了我这个错误:

未捕获的异常 '学说\ORM\Mapping\MappingException' 带有消息“无标识符/主要 为实体“B”指定的密钥。每一个 实体必须有一个标识符/主要 钥匙。'在 /var/www/agr-reg-php/Doctrine/ORM/Mapping/MappingException.php:37 堆栈跟踪:

注意:我不能有复合主键。

I have the two tables :

table A with id as primary key

table B with id as primary key and foreign key

Explanation on short:

I need to have in table B a primary key that also to be a foreign key that points to table A's primary key.

Can anybody explain me how to map this by annotations in Doctrine 2?

Note:

I tried it By this :

   class A
{
    /**
     * @var bigint $id
     *
     * @Column(name="id", type="bigint", nullable=false)
     * @Id
     * @GeneratedValue(strategy="IDENTITY")
     */
    private $a_id;
...

and B table:


class B
{
    /**
     * @var bigint $id
     * @Id 
     * @OneToOne(targetEntity="A", fetch="LAZY")
     * @JoinColumn(name="id", referencedColumnName="id")
     */
    private $b_id;
...

But it gives me this error:

Uncaught exception
'Doctrine\ORM\Mapping\MappingException'
with message 'No identifier/primary
key specified for Entity 'B'. Every
Entity must have an identifier/primary
key.' in
/var/www/agr-reg-php/Doctrine/ORM/Mapping/MappingException.php:37
Stack trace:

N.B: I must not have composite primary key.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

提笔书几行 2024-11-23 01:17:25

Doctrine 2.1以来,这是可能的:

通过外部实体或派生实体进行身份识别:您现在可以使用外键作为实体的标识符。这转化为在 @ManyToOne 或 @OneToOne 关联上使用 @Id。您可以阅读此 教程中的功能

This is possible since Doctrine 2.1:

Identity through Foreign Entities or derived entities: You can now use a foreign key as identifier of an entity. This translates to using @Id on a @ManyToOne or @OneToOne association. You can read up on this feature in the tutorial.

热风软妹 2024-11-23 01:17:25

我可以解决这个问题,创建一个与外部字段同名的 pk 字段

class B
{
/**
 * @var bigint $a_id
 * @Id @Column(name="a_id", type="bigint", nullable="false")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $a_id;

/**
 * @OneToOne(targetEntity="A", fetch="LAZY")
 * @JoinColumn(name="id", referencedColumnName="id")
 */
private $a;
.
.
.
/**
 * @var A
 *
 * @ORM\OneToOne(targetEntity="A")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="a_id", referencedColumnName="id", unique=true)
 * })
 */
private $a;
//...

I could solve the problem, creating a pk field with the same name to the foreign field

class B
{
/**
 * @var bigint $a_id
 * @Id @Column(name="a_id", type="bigint", nullable="false")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $a_id;

/**
 * @OneToOne(targetEntity="A", fetch="LAZY")
 * @JoinColumn(name="id", referencedColumnName="id")
 */
private $a;
.
.
.
/**
 * @var A
 *
 * @ORM\OneToOne(targetEntity="A")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="a_id", referencedColumnName="id", unique=true)
 * })
 */
private $a;
//...
败给现实 2024-11-23 01:17:25

我有同样的任务并通过实验找到了这个解决方案:

class A {  
    /**  
    * @Id @Column(type="integer", nullable=false)  
    * @GeneratedValue  
    */  
    protected $id;  

    /**  
     * @OneToOne(targetEntity="B", inversedBy="a", orphanRemoval=true, cascade={"persist", "remove"})  
     * @JoinColumn(name="id", referencedColumnName="id")  
     */  
    protected $b;  

}  

class B {  
    /**  
     * @OneToOne(targetEntity="A", mappedBy="b" )  
     */  
    protected $user;  

    /**  
     * @Id  
     * @Column(type="integer", nullable=false)  
     * @GeneratedValue  
     */  
    protected $id;  

}  

I had the same task and experimentaly found this solution:

class A {  
    /**  
    * @Id @Column(type="integer", nullable=false)  
    * @GeneratedValue  
    */  
    protected $id;  

    /**  
     * @OneToOne(targetEntity="B", inversedBy="a", orphanRemoval=true, cascade={"persist", "remove"})  
     * @JoinColumn(name="id", referencedColumnName="id")  
     */  
    protected $b;  

}  

class B {  
    /**  
     * @OneToOne(targetEntity="A", mappedBy="b" )  
     */  
    protected $user;  

    /**  
     * @Id  
     * @Column(type="integer", nullable=false)  
     * @GeneratedValue  
     */  
    protected $id;  

}  
陌生 2024-11-23 01:17:25

最后,我通过在实体类中为真实表中的同一列指定两个字段来解决我的问题。这些更改仅在 B 类中进行(查看 A 类的问题):


class B
{
    /**
     * @var bigint $id
     * @Id @Column(name="id", type="bigint", nullable="false")
     */
    private $b_id;

    /**
     * @OneToOne(targetEntity="A", fetch="LAZY")
     * @JoinColumn(name="id", referencedColumnName="id")
     */
    private $a;

...

事实上,我所做的就是在我的实体中为相同的主键和外键写入两个字段。

Finally I resolved my problem by specifying two fields in my entity class for the same column from real table. The changes are made only in class B (look at the question for class A):


class B
{
    /**
     * @var bigint $id
     * @Id @Column(name="id", type="bigint", nullable="false")
     */
    private $b_id;

    /**
     * @OneToOne(targetEntity="A", fetch="LAZY")
     * @JoinColumn(name="id", referencedColumnName="id")
     */
    private $a;

...

In fact all what I have done is write two fields in my entity for the same primary key and foreign key.

謸气贵蔟 2024-11-23 01:17:25

嗯,这是插入的另一种临时解决方案:

    /**
     * @Entity
     * @Table(name="types")
     */
    class Type {
    /**
     * @Id
     * @Column(type="integer")
     * @GeneratedValue
     */
    private $id;
    /**
     * @OneToMany(targetEntity="type\models\Language", mappedBy="type")
     */
    private $languages;

    /**
     * @Column(type="string", length = 45,nullable = true)
     */
    private $category;

    /**
     * @Column(type="string", length = 1)
     */
    private $status;

    /**
     * @Column(type="string", length = 45)
     */
    private $name;

    /**
     * @Column(type="datetime", nullable = true)
     */
    private $datedeleted;

    /**
     * @Column(type="datetime", nullable = true)
     */
    private $datemodificated;

    /**
     * @Column(type="datetime")
     */
    private $dateregistered;

    public function __construct() {
        $this->languages = new \Doctrine\Common\Collections\ArrayCollection;
        $this->status = 1;
        $this->dateregistered = new \DateTime("now");
    }

    public function id() {
            return $this->id;
    }

    public function category($value = NULL) {
        if (is_null($value))
            return $this->category;
        else
            $this->category = $value;
    }

    public function name($value = NULL) {
        if (is_null($value))
            return $this->name;
        else
            $this->name = $value;
    }

    public function status($value = NULL) {
        if (is_null($value))
            return $this->status;
        else
            $this->status = $value;
    }

    public function datedeleted($value = NULL) {
        if (is_null($value))
            return $this->datedeleted;
        else
            $this->datedeleted = $value;
    }

    public function datemodificated($value = NULL) {
        if (is_null($value))
            return $this->datemodificated;
        else
            $this->datemodificated = $value;
    }

    public function dateregistered($value = NULL) {
        if (is_null($value))
            return $this->dateregistered;
        else
            $this->dateregistered = $value;
        }
    }

    /**
     * @Entity
     * @Table(name="languages")
     */
    class Language {

    /**
     * @Id 
     * @Column(name="type_id", type="integer", nullable="false")
     */
    private $language_id;

    /**
     * @Id
     * @ManyToOne(targetEntity="type\models\Type",inversedBy="languages")
     * @JoinColumn(name="type_id", referencedColumnName="id")
     */
    private $type;

    /**
     * @Column(type="string", length = 100, nullable = true)
     */
    private $description;

    /**
     * @Id
     * @Column(type="string", length = 20)
     */
    private $language;

    public function language_id($value) {
        $this->language_id = $value;
    }

    public function description($value = NULL) {
        if (is_null($value))
            return $this->description;
        else
            $this->description = $value;
    }

    public function language($value = NULL) {
        if (is_null($value))
            return $this->language;
        else
            $this->language = $value;
    }

    public function type($value = NULL) {
        if (is_null($value))
            return $this->type;
        else
            $this->type = $value;


     }
    }

$language = new Language;
$xtype_id = $this->em->find("Type",1);
$language->language_id($xtype_id->id());
$language->type($xtype_id);
$language->description($xdescription);
$language->language($xlanguage);
$this->em->persist($this);
$this->em->flush();

在具有语言中的外键和主键的类中插入

Well this is other temporal solution for insert:

    /**
     * @Entity
     * @Table(name="types")
     */
    class Type {
    /**
     * @Id
     * @Column(type="integer")
     * @GeneratedValue
     */
    private $id;
    /**
     * @OneToMany(targetEntity="type\models\Language", mappedBy="type")
     */
    private $languages;

    /**
     * @Column(type="string", length = 45,nullable = true)
     */
    private $category;

    /**
     * @Column(type="string", length = 1)
     */
    private $status;

    /**
     * @Column(type="string", length = 45)
     */
    private $name;

    /**
     * @Column(type="datetime", nullable = true)
     */
    private $datedeleted;

    /**
     * @Column(type="datetime", nullable = true)
     */
    private $datemodificated;

    /**
     * @Column(type="datetime")
     */
    private $dateregistered;

    public function __construct() {
        $this->languages = new \Doctrine\Common\Collections\ArrayCollection;
        $this->status = 1;
        $this->dateregistered = new \DateTime("now");
    }

    public function id() {
            return $this->id;
    }

    public function category($value = NULL) {
        if (is_null($value))
            return $this->category;
        else
            $this->category = $value;
    }

    public function name($value = NULL) {
        if (is_null($value))
            return $this->name;
        else
            $this->name = $value;
    }

    public function status($value = NULL) {
        if (is_null($value))
            return $this->status;
        else
            $this->status = $value;
    }

    public function datedeleted($value = NULL) {
        if (is_null($value))
            return $this->datedeleted;
        else
            $this->datedeleted = $value;
    }

    public function datemodificated($value = NULL) {
        if (is_null($value))
            return $this->datemodificated;
        else
            $this->datemodificated = $value;
    }

    public function dateregistered($value = NULL) {
        if (is_null($value))
            return $this->dateregistered;
        else
            $this->dateregistered = $value;
        }
    }

    /**
     * @Entity
     * @Table(name="languages")
     */
    class Language {

    /**
     * @Id 
     * @Column(name="type_id", type="integer", nullable="false")
     */
    private $language_id;

    /**
     * @Id
     * @ManyToOne(targetEntity="type\models\Type",inversedBy="languages")
     * @JoinColumn(name="type_id", referencedColumnName="id")
     */
    private $type;

    /**
     * @Column(type="string", length = 100, nullable = true)
     */
    private $description;

    /**
     * @Id
     * @Column(type="string", length = 20)
     */
    private $language;

    public function language_id($value) {
        $this->language_id = $value;
    }

    public function description($value = NULL) {
        if (is_null($value))
            return $this->description;
        else
            $this->description = $value;
    }

    public function language($value = NULL) {
        if (is_null($value))
            return $this->language;
        else
            $this->language = $value;
    }

    public function type($value = NULL) {
        if (is_null($value))
            return $this->type;
        else
            $this->type = $value;


     }
    }

$language = new Language;
$xtype_id = $this->em->find("Type",1);
$language->language_id($xtype_id->id());
$language->type($xtype_id);
$language->description($xdescription);
$language->language($xlanguage);
$this->em->persist($this);
$this->em->flush();

this insert in a Class With the foreign key and primary key in language

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文