How to use auto increment for custom field in data object


Hi All,

I would like to have a field jobid in my data object which should be a number, unique and auto generated on object creation. The field will be read only for pimcore users.

For this case I thought to use the auto increment mysql feature on a standard numeric data object field. But I don’t know how to add the auto increment feature for this field in my bundle, because I don’t want to add the feature manually via phpmyadmin.

The job class is imported via bundle installer which works fine.
I already tried an eventListener on object Add event but I didn’t know how to generate a unique, increment number in this state in php so I moved to the auto increment solution.

Do you have an idea?

Thank you in advance for any help!

Kind regards,


Would PHP’s uniquid function help here (used in your event listener)?

You could use the prefix and entropy fields to increase the likelihood of values being unique.

If the job numbers need to be consecutive you could try a MySQL variable, e.g.

initialize the variable once:
SET @job_no := 0;

then in event listener select the next value (and increment) using a mysql statement to set the job number field in your data object:
SELECT (@job_no := @job_no + 1);


Thank you for your reply.

I solved it by writing a method in my bundle installer class which calls my function insertDump which does the following:

// Pimcore\Model\Tool\Setup
$setup = new Setup();
$setup->insertDump($this->installSourcesPath . ‘/sql/install.sql’);

In the sql file I put an alter table statement and added the auto increment feature.

Kind regards,


Not the best idea. I am afraid thats not actually possible to do with the AUTO_INCREMENT. Pimcore doesn’t currently allow to change the column creation. What you can do is:

Create a PRE_CREATE Listener for your object and use a separate table or configuration or something to store your ID. Increment that on every use and store it into the object.