9

In doctrine2 I have an entity that has a primary key that's feeded from a webservice, and also has an index that should be an auto increment.

I can set manually in mysql but can't make this work in doctrine2.

Cœur
  • 37,241
  • 25
  • 195
  • 267
jmarcone
  • 118
  • 1
  • 1
  • 6

2 Answers2

23

I used columnDefinition of INT AUTO_INCREMENT UNIQUE

/**
 * @var integer
 *
 * @ORM\Column(type="integer", name="sequence", nullable=true, columnDefinition="INT AUTO_INCREMENT UNIQUE")
 */
protected $sequence = null;

Doctrine migration bundle generates

$this->addSql('ALTER TABLE table_name_here ADD sequence INT AUTO_INCREMENT UNIQUE');

Edit: Note that this answer is from 2016, it may no longer be valid. See accepted answer.

gingerCodeNinja
  • 1,239
  • 1
  • 12
  • 27
  • 2
    still valid on doctrine/orm v2.6.2 – slk500 Aug 13 '18 at 14:24
  • If I use `columnDefinition` and insert a new record into the table, will the entity be updated with the autogenerated autoincremented value? – Meglio Mar 19 '20 at 07:59
  • Also, `columnDefinition` leads to a situation when `doctrine:migrations:diff` generates the same SQL statement in both UP and DOWN migrations, e.g. `ALTER TABLE users_interests CHANGE id id BIGINT AUTO_INCREMENT NOT NULL ` - this query is the same in both UP and DOWN migrations. – Meglio Mar 19 '20 at 14:37
  • This is so great, simple and still works. – Bartłomiej Jakub Kwiatek Apr 12 '22 at 10:21
7

The auto-increment limitation is related to the database you are using. in Mysql ,Mysql_autoincrement, it also depends on the engine you are using. exemple:

For MyISAM and BDB tables you can specify AUTO_INCREMENT on a secondary column in a multiple-column index. In this case, the generated value for the AUTO_INCREMENT column is calculated as

You can have a general documentation on auto_increment here

In general the auto_increment is reserved to a numerical field that is part of the identifier index (one or more column involved).

So this can work for mysql.

However the auto_incriment in doctrine seems to only be alllowed for THe @Id (Primary Key)

21.2.9. @GeneratedValue

Specifies which strategy is used for identifier generation for an instance variable which is annotated by @Id. This annotation is optional and only has meaning when used in conjunction with @Id.

If this annotation is not specified with @Id the NONE strategy is used as default.

Required attributes:

strategy: Set the name of the identifier generation strategy. Valid values are AUTO, SEQUENCE, TABLE, IDENTITY, UUID, CUSTOM and NONE. Example:

?php
/**
 * @Id
 * @Column(type="integer")
 * @GeneratedValue(strategy="AUTO")
 */
protected $id = null;

Doctrine_auto_increment

You may want to find a work arround and post a ticket about it

Jeffrey Nicholson Carré
  • 2,950
  • 1
  • 26
  • 44
  • @MatthieuNapoli at the time yes but 6 years later i am not sure, but it seems that Auto is still based on what is availabl on Mysql. Maybe what you need is UUID strategy ? . Check this link : https://stackoverflow.com/a/14029064/1427338 – Jeffrey Nicholson Carré Jan 25 '19 at 00:45