Gottlieb Notschnabel Gottlieb Notschnabel - 4 months ago 10
SQL Question

How can I add an INDEX with Doctrine 2 to a column without making it a primary key?

I want to add an

index
to a table column in my MySQL-database. I am using Doctrine 2 to create my database schemes.

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).

What is the proper statement to create such an index?

Answer

If you want to work with doctrine a table must have a primary key, see: http://docs.doctrine-project.org/en/latest/reference/basic-mapping.html#identifiers-primary-keys

To create an index: http://docs.doctrine-project.org/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.

Comments