rabol's avatar
Level 14

How to implement sequential numbers for records

Hi

Is there a easy way to implement sequential numbering for records?

What I need is:

on a table i have a string field, let's say seqId

and in every form I would like to initialise the field with the next number in in the sequence, but as the same time format it like INV-000001, INV-000002 etc.

It should be DB independent :)

Thanks in advance

0 likes
4 replies
SaeedPrez's avatar

Hi @rabol, try this..

for ($nr = 1; $nr <= 25; $nr++) {
    echo str_pad($nr, 10, 'INV-000000', STR_PAD_LEFT);
}
rabol's avatar
Level 14

yes, but the $nr is the one that I need, not the formatting :)

in good old ADO PHP, there was a GenId()

/**
 * Generates a sequence id and stores it in $this->genID;
 * GenID is only available if $this->hasGenID = true;
 *
 * @param seqname       name of sequence to use
 * @param startID       if sequence does not exist, start at this ID
 * @return      0 if not supported, otherwise a sequence id
 */

I was hoping that Laravel had something similar

kobear's avatar

A couple of different ways you can do this:

  1. Using an incrementing integer primary key, and using that to build your string field. Should be easy to do.

  2. Create a new table for numbers, and have that table track your number creation. More work to do.

rabol's avatar
Level 14

hmmm doctrine2/lib/Doctrine/ORM/Id/SequenceGenerator.php

but that is not part of Laravel

Please or to participate in this conversation.