MySQL 如何将带有 Doctrine 2 的 INDEX 添加到列而不使其成为主键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17991782/
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
How can I add an INDEX with Doctrine 2 to a column without making it a primary key?
提问by Gottlieb Notschnabel
I want to add an index
to a table column in my MySQL-database. I am using Doctrine 2 to create my database schemes.
我想index
在我的 MySQL 数据库中的表列中添加一个。我正在使用 Doctrine 2 创建我的数据库方案。
I know that I can use
我知道我可以使用
/** @Id @Column(type="integer") */
to create primary keys. But my column shall neither have the unique
nor the primary key
attribute. It shall simply be an index
in my table (MySQL knows these three types).
创建主键。但我的专栏不应有 theunique
或 theprimary key
属性。它应该只是index
我的表中的一个(MySQL 知道这三种类型)。
What is the proper statement to create such an index?
创建这样的索引的正确语句是什么?
回答by Flip
If you want to work with doctrine a table must have a primary key, see:
如果你想使用学说,一个表必须有一个主键,请参阅:
Every entity class must have an identifier/primary key. You can select the field that serves as the identifier with the
@Id
annotation.
Reference: https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html#identifiers-primary-keys
每个实体类都必须有一个标识符/主键。您可以选择用作带有
@Id
注释的标识符的字段。
参考:https: //www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html#identifiers-primary-keys
To create an index: https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#annref_index
创建索引:https: //www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#annref_index
<?php
/**
* @Entity
* @Table(name="ecommerce_products",indexes={@Index(name="search_idx", columns={"name", "email"})})
*/
class ECommerceProduct
{
}
Note that this is only being used if you generate the schema from your php code. So in case your table already exist you can also just add an index yourself.
请注意,这仅在您从 php 代码生成架构时使用。因此,如果您的表已经存在,您也可以自己添加索引。
回答by websky
When you use doctrine and orm:
当您使用学说和 orm 时:
@ORM\Table(indexes={@ORM\Index(name="name_idx", columns={"name"})})
回答by Samir Patel
If you're using Symfony with doctrine, you will also need to use
the Index
class in order for the annotation to work properly
use Doctrine\ORM\Mapping\Index;
如果您使用Symfony的教义,你还需要use
的Index
类,以使注释正常工作
use Doctrine\ORM\Mapping\Index;
回答by Vasilii Suricov
Part of working code
部分工作代码
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="AppBundle\Repository\CountryRepository")
* @ORM\Table(indexes={@ORM\Index(columns={"slug"})})
*/
class Country extends AbstractTrans implements SuggesterItem