Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine 2 with multiple indexes

I am developing using zend framework and doctrine2.1.

I have generated entities from database.

But the problem is: Doctrine doesn't recognize my indexes. They are not marked in entity annotations at all.

And when I go to validate-schema and dump sql from orm:schema-tool:update --dump-sql it generates sql to drop all my indexes across whole database.

I found that Doctrine has following annotation used for defining indexes:

indexes={@index(name="index_name",
                columns={"database_column1","database_column2"}
        )}

But this allows me to define one index for multiple columns and I don't really need that.
What I want is the ability to define multiple indexes on multiple columns, one index per column.

Is there a way I can achieve this? Is there a way that I can have annotation that defines multiple indexes.

like image 729
dennisg Avatar asked Nov 15 '11 18:11

dennisg


2 Answers

I would say you can insert multiple indexes in the indexes property (but I haven't had the time to test it):

indexes={
@ORM\Index(name="index_name", columns={"database_column1","database_column2"}),
@ORM\Index(name="index_name2", columns={"database_column1"}),
@ORM\Index(name="index_name3", columns={"database_column2"})
}

Hope this helps you

like image 181
Kristian Zondervan Avatar answered Oct 20 '22 07:10

Kristian Zondervan


Here is an example:

/**
 * @Entity
 * @Table(name="serial_number",indexes={
 *  @index(name="product_idx", columns={"product_id"}),
 * })
 */
class SerialNumber { // Entity Class

    /**
     * @var int
     * 
     * @Id
     * @GeneratedValue
     * @Column(type="integer")
     */

    protected $id;

    /**
     * @Column(name="created_at", type="datetime")
     * @var \DateTime
     * */
    protected $created;

    /**
     * @Column(name="updated_at", type="datetime")
     * @var \DateTime
     * */
    protected $updated;

    /**
     * @Column(name="product_id", type="integer")
     */
    protected $productID;

}
like image 30
David Avatar answered Oct 20 '22 08:10

David