php Doctrine 和复合唯一键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12641698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:57:09  来源:igfitidea点击:

Doctrine and composite unique keys

phpmysqldoctrine-orm

提问by Nikoole

I want to do composite unique key in doctrine. Those are my fields:

我想在学说中做复合唯一键。这些是我的领域:

/**
 * @var string $videoDimension
 *
 * @Column(name="video_dimension", type="string", nullable=false)
 */
private $videoDimension;

/**
 * @var string $videoBitrate
 *
 * @Column(name="video_bitrate", type="string", nullable=false)
 */
private $videoBitrate;

How can I show doctrine, that those combined together are composite unique key?

我如何展示教义,即这些组合在一起是复合唯一键?

回答by Nikoole

Answer the question:

回答问题:

use Doctrine\ORM\Mapping\UniqueConstraint;

/**
 * Common\Model\Entity\VideoSettings
 *
 * @Table(name="video_settings", 
 *    uniqueConstraints={
 *        @UniqueConstraint(name="video_unique", 
 *            columns={"video_dimension", "video_bitrate"})
 *    }
 * )
 * @Entity
 */

See @UniqueConstraint

@UniqueConstraint

回答by luchaninov

I find it more verbose to useonly ORM and then prefix ORMin annotations. Also note that you can break annotation to several lines to make it more readable especially if you have several items to mention (index in the example below).

我发现use只有 ORM更冗长,然后ORM在注释中添加前缀。另请注意,您可以将注释拆分为多行以使其更具可读性,尤其是当您要提及多个项目时(下例中的索引)。

use Doctrine\ORM\Mapping as ORM;

/**
 * VideoSettings
 *
 * @ORM\Cache(usage="NONSTRICT_READ_WRITE")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\VideoSettingsRepository")
 * @ORM\Table(name="emails", uniqueConstraints={
 *      @ORM\UniqueConstraint(name="dimension_bitrate", columns={"video_dimension", "video_bitrate"})
 * }, indexes={
 *      @ORM\Index(name="name", columns={"name"})
 * })
 */
class VideoSettings

回答by Stas Parshyn

I know this is an old question, but I came across it while looking for a way to create composite PK and thought it could use some update.

我知道这是一个老问题,但我在寻找创建复合 PK 的方法时遇到了它,并认为它可以使用一些更新。

Things are actually much simpler if what you need is a Composite Primary Key. (Which, of course, guarantees uniqueness) Doctrine documentation contains some nice examples by this url: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/composite-primary-keys.html

如果您需要的是复合主键,事情实际上要简单得多。(当然,这保证了唯一性)Doctrine 文档通过这个 url 包含了一些很好的例子:http: //docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/composite-primary-keys.html

So the original example could look something like this:

因此,原始示例可能如下所示:

/**
 * @var string $videoDimension
 *
 * @ORM\Id @ORM\Column(type="string")
 */
private $videoDimension;

/**
 * @var string $videoBitrate
 *
 * @ORM\Id @ORM\Column(type="string")
 */
private $videoBitrate;

A few notes here:

这里有一些注意事项:

  1. Column "name" is omitted since Doctrine is able to guess it based on the property name
  2. Since videoDimensionand videoBitrateare both parts of the PK - there is no need to specify nullable = false
  3. If required - the Composite PK may be composed of foreign keys, so feel free to add some relational mappings
  1. 列“名称”被省略,因为 Doctrine 能够根据属性名称猜测它
  2. 由于videoDimensionvideoBitrate都是 PK 的一部分 - 无需指定nullable = false
  3. 如果需要 - Composite PK 可能由外键组成,因此可以随意添加一些关系映射