Generated integer ID - set custom value

Hello!

The following initial situation: I have an object “Shipping” with a unique, consecutive number (NumberShipping).

/**
 * @Flow\Entity
 */
class Shipping
{
    /**
     * @var NumberShipping
     * @ORM\OneToOne(cascade={"persist", "remove"})
     * @ORM\Column(nullable=true)
     */
    protected $number;

/**
 * @Flow\Entity
 */
class NumberShipping
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @var int
     */
    protected $number;

    /**
     * @return int
     */
    public function getNumber(): int
    {
        return $this->number;
    }

    /**
     * @param int $number
     */
    public function setNumber(int $number): void
    {
        $this->number = $number;
    }
}

This works as far as it should. The numbers are generated ascending and unique. Now I want to give the user the possibility to increase the numbers. So he can specify that the number range starts at 10.000. I have implemented this as follows:

/**
 * @Flow\Scope("singleton")
 */
class NumberShippingRepository extends Repository
{
    protected $defaultOrderings = ["number" => QueryInterface::ORDER_DESCENDING];

    /**
     * @param int $nextNumber
     * @throws \Neos\Flow\Persistence\Exception\IllegalObjectTypeException
     */
    public function setNextNumber(int $nextNumber) {
        $nextNumber -= 1;

        /** @var NumberShipping $lastUsedNumberObject */
        $lastUsedNumberObject = $this->createQuery()->setLimit(1)->execute()->getFirst();
        $lastUsedNumber = $lastUsedNumberObject->getNumber();

        if ($lastUsedNumber >= $nextNumber)
            return;

        $newNumber = new NumberShipping();
        $newNumber->setNumber($nextNumber);

        $this->add($newNumber);
    }
}

Now the problem is, if $nextNumber has the value 10.000, a new instance of NumberShipping is created, but the counting method is unaffected. (so it is counted up normally). Can I force Doctrine (or whoever) to use my 10,000?

Thanks for your help!

The GenerateValue method uses the AUTO_INCREMENT table setting in case of MySQL by default (refer to the Doctrine Documention on that). I don’t think there is a Doctrine-Way to manipulate it afterwards.

“Knowing” that you have MySQL and wanting to break portability to other DB systems, you could simply apply a raw SQL statement to increase the current AUTO_INCREMENT value:

ALTER TABLE number_shipping AUTO_INCREMENT=$number

Other alternatives would be to use another generator instead of AUTO for example CUSTOM and implement your own way of generating identifiers.