Symfony2 OneToOne关系成为唯一索引或外键? [英] Symfony2 OneToOne relationship becomes either a Unique index or a foreign key?

查看:180
本文介绍了Symfony2 OneToOne关系成为唯一索引或外键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Symfony2实体关系中迈出我的第一步。
我有一个实体安装,每个都有一米,一个监视器。



这转换为一个单向关系,我定义如下:

  / ** 
*
* @ ORM\OneToOne(targetEntity =InfoMeter)
* @ ORM\JoinColumn(name =meterid,referencedColumnName =id)
* /
private $ meter;

/ **
*
* @ ORM\OneToOne(targetEntity =InstallationsRtu)
* @ ORM\JoinColumn(name =monitorid referencedColumnName =id)
* /
private $ monitor;

每个显示器只能分配到一个安装。
每个电表可以分配到多个安装。



当我更新我的数据库(app / console doctrine:schema:update --force)时,有两个结果。

如果是监视器:

一切顺利,模式更新为一个以UNIQ _为前缀的键名。



如果是米:
我收到以下错误

  PDOException] 
SQLSTATE [23000]:完整性限制违规:1062密钥UNIQ_43D26968860BE41D的重复条目'1'

我看到这个错误发生后的表结构,我找不到提到的约束,但我确实发现FK_43D26968860BE41D,即相同,但前缀为'FK'。



在安装表中,我现在列出了这些:

 键名类型唯一的打包字段基数
PRIMARY BTREE是否id 2
UNIQ_43D26968701EC1AB BTREE是否监控2
FK_43D26968860BE41D BTREE否否米特2

所以说Unique = Yes,另一个说Unique = No。



要达到我的问题:

如何判断是UNIQ索引还是FK索引?
我假设Doctrine看到当前每个monitorid在安装表中是唯一的,但是每个meterid在安装表中都会发生多次。

因此,它与UNIQ一起出现,FK对于后者。但我可以控制这个吗?

解决方案

如果一个Metter可以分配给多个安装,你不应该定义OneToMany关系?



在您的安装实体中:

  / ** 
* @ ORM\ ManyToOne(targetEntity =InfoMeter,inversedBy =installed)
* @ ORM\JoinColumn(name =infometer_id,referencedColumnName =id)
* /
protected $ info_meter ;

然后在您的InfoMeter实体中:

  / ** 
* @ ORM\OneToMany(targetEntity =安装,mappedBy =info_meter)
* /
protected $

此外,您应该将以下内容添加到InfoMeter类构造函数中:

  function __construct(){
[...]
$ this-> installed = new \Doctrine\Common\ Collections\ArrayCollection();
}

我确信这种方法可以根据你想要的关系如何改善在安装和米之间,但这应该工作。


I'm taking my first steps in Symfony2 entity relations. I have an entity installation, which each has one meter, and one monitor.

This translates to a uni-directional relationship, which I defined as such:

    /**
     *
     * @ORM\OneToOne(targetEntity="InfoMeter")
     * @ORM\JoinColumn(name="meterid", referencedColumnName="id")
     */
    private $meter;  

    /**
     *
     * @ORM\OneToOne(targetEntity="InstallationsRtu")
     * @ORM\JoinColumn(name="monitorid", referencedColumnName="id")
     */
    private $monitor;

Each monitor can only be assigned to one installation. Each meter can be assigned to multiple installations.

When I update my database (app/console doctrine:schema:update --force), there are two outcomes.
In case of the monitor:
Everything goes alright, schema gets updated with a keyname which is prefixed by 'UNIQ_'.

In case of the meter: I get the following error

PDOException]                                                                                             
  SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'UNIQ_43D26968860BE41D'

When I look at the table structure after this error occured, I can't find that mentioned constraint, but I do find FK_43D26968860BE41D, i.e. the same, but prefixed with 'FK'.

In the installation table, I now have these listed:

Keyname                 Type    Unique  Packed  Field     Cardinality
PRIMARY                 BTREE   Yes No  id    2 
UNIQ_43D26968701EC1AB   BTREE   Yes No  monitorid 2     
FK_43D26968860BE41D BTREE   No  No  meterid   2

So one saying 'Unique=Yes', and the other saying 'Unique=No'.

To arrive at my question:
How can I decide whether it is a UNIQ index or a FK index? I assume that Doctrine saw that currently each monitorid is unique in the installation table, but that each meterid occurs several times in the installation table.
Hence it went with UNIQ for the first, and FK for the latter. But can I control this somehow?

解决方案

If one metter can be assigned to multiple installations, shouldn't you define a OneToMany relationship?

In your installation entity:

/**
 * @ORM\ManyToOne(targetEntity="InfoMeter", inversedBy="installations")
 * @ORM\JoinColumn(name="infometer_id", referencedColumnName="id")
 */
protected $info_meter;

And then in your InfoMeter entity:

/**
 * @ORM\OneToMany(targetEntity="Installation",mappedBy="info_meter")
 */
protected $installations;

Besides, you should add the following to your InfoMeter class constructor:

function __construct() {
   [...]
   $this->installations = new \Doctrine\Common\Collections\ArrayCollection();
}

I'm sure this approach can be improved depending on how you want the relation between "installations" and "meters", but this should work.

这篇关于Symfony2 OneToOne关系成为唯一索引或外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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