Doctrine 2 with multiple indexes

2019-03-12 03:42发布

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.

2条回答
做个烂人
2楼-- · 2019-03-12 04:25

I would say you can insert multiple indexes in the indexes property (but didn'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

查看更多
霸刀☆藐视天下
3楼-- · 2019-03-12 04:41

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;

}
查看更多
登录 后发表回答