Home > OS >  Doctrine / QueryBuilder: how to change auto-increment counter?
Doctrine / QueryBuilder: how to change auto-increment counter?

Time:01-06

I have a Nextcloud server with a DBMS backend. I need to alter the auto-increment counter of the next entry's ID in a table on the DBMS.

One use for this is to change the invoice numbering at the beginnging of the year (i.e. starting with 202300000 this year). Another use is after a migration. I need to keep the IDs of source entries so the next-ID counter may be wrong. Upon the next insert, the counter is incremented from the old value and an error is thrown because the new ID already exists.

On mySQL I would do a

ALTER TABLE oc_myapp_mytable AUTO_INCREMENT = 20;

On Postgresql (my DB of choice for now) I would do the same with

ALTER SEQUENCE oc_myapp_mytable_id_seq RESTART WITH 20;

However, I am reluctant to use direct DB manipulation and seek for an abstracted Doctrine / QueryBuilder approach.

Is there a way to alter the next increment value through Doctrine DB-agnostically?

If not, how can I issue a direct command to the DB through QueryBuilder? Can QueryBuilder figure out the DB type of the backend? Come to that, how can the DB type be determined over a mysqli adapter from PHP?

CodePudding user response:

There is no built in way of inrementing an autoincrement value in an "abstracted" way (I am aware of).

I assume with query builder you mean "DBAL" - there you can retrieve the current platform name:

https://www.doctrine-project.org/projects/doctrine-dbal/en/current/reference/platforms.html#platforms

$platform = $conn->getDatabasePlatform();

Then you can act on this information, and set the counter programmatically.

  • Related