如何将一个带有Doctrine 2的INDEX添加到列而不将其作为主键?

时间:2013-08-01 10:41:46

标签: mysql indexing doctrine-orm

我想在MySQL数据库的表列中添加index。我正在使用Doctrine 2来创建我的数据库方案。

我知道我可以使用

/** @Id @Column(type="integer") */

创建主键。但我的专栏既没有unique也没有primary key属性。它应该只是我表中的index(MySQL知道这三种类型)。

创建此类索引的正确陈述是什么?

4 个答案:

答案 0 :(得分:50)

如果您想使用教条,表必须有主键,请参阅:http://docs.doctrine-project.org/en/latest/reference/basic-mapping.html#identifiers-primary-keys

创建索引: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
{
}

请注意,仅当您从php代码生成架构时才会使用此选项。因此,如果您的表已经存在,您也可以自己添加一个索引。

答案 1 :(得分:25)

当你使用doctrine和orm时:

@ORM\Table(indexes={@ORM\Index(name="name_idx", columns={"name"})})

答案 2 :(得分:4)

如果你将Symfony与学说一起使用,你还需要use Index类才能使注释正常工作 use Doctrine\ORM\Mapping\Index;

答案 3 :(得分:2)

部分工作代码

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