DDC-450: Add TableGenerator Implementation #562

Closed
opened 2026-01-22 12:42:38 +01:00 by admin · 6 comments
Owner

Originally created by @doctrinebot on GitHub (Mar 20, 2010).

Jira issue originally created by user @beberlei:

The TableGenerator Id Generator is not yet implemented, here is some code i came up with:

class TableGenerator extends AbstractIdGenerator
{
    private $_tableName;
    private $_sequenceName;
    private $_allocationSize;
    private $_nextValue;
    private $_maxValue;

    public function **construct($tableName, $sequenceName = 'default', $allocationSize = 10)
    {
        $this->_tableName = $tableName;
        $this->_sequenceName = $sequenceName;
        $this->_allocationSize = $allocationSize;
    }

    public function generate(EntityManager $em, $entity)
    {
        if ($this->*maxValue === null || $this->_nextValue == $this->*maxValue) {
            // Allocate new values
            $conn = $em->getConnection();
            if ($conn->getTransactionNestingLevel() == 0) {

                // use select for update
                $sql = $conn->getDatabasePlatform()->getTableHiLoCurrentValSql($this->*tableName, $this->*sequenceName);
                $currentLevel = $conn->fetchColumn($sql);
                if ($currentLevel != null) {
                    $this->_nextValue = $currentLevel;
                    $this->*maxValue = $this->_nextValue <ins> $this->*allocationSize;

                    $updateSql = $conn->getDatabasePlatform()->getTableHiLoUpdateNextValSql(
                        $this->*tableName, $this->_sequenceName, $this->*allocationSize
                    );

                    if ($conn->executeUpdate($updateSql, array(1 => $currentLevel, 2 => $currentLevel</ins>1)) !== 1) {
                        // no affected rows, concurrency issue, throw exception
                    }
                } else {
                    // no current level returned, TableGenerator seems to be broken, throw exception
                }
            } else {
                // only table locks help here, implement this or throw exception?
                // or do we want to work with table locks exclusively?
            }
        }
        return $this->_nextValue<ins></ins>;
    }
}
Originally created by @doctrinebot on GitHub (Mar 20, 2010). Jira issue originally created by user @beberlei: The TableGenerator Id Generator is not yet implemented, here is some code i came up with: ``` class TableGenerator extends AbstractIdGenerator { private $_tableName; private $_sequenceName; private $_allocationSize; private $_nextValue; private $_maxValue; public function **construct($tableName, $sequenceName = 'default', $allocationSize = 10) { $this->_tableName = $tableName; $this->_sequenceName = $sequenceName; $this->_allocationSize = $allocationSize; } public function generate(EntityManager $em, $entity) { if ($this->*maxValue === null || $this->_nextValue == $this->*maxValue) { // Allocate new values $conn = $em->getConnection(); if ($conn->getTransactionNestingLevel() == 0) { // use select for update $sql = $conn->getDatabasePlatform()->getTableHiLoCurrentValSql($this->*tableName, $this->*sequenceName); $currentLevel = $conn->fetchColumn($sql); if ($currentLevel != null) { $this->_nextValue = $currentLevel; $this->*maxValue = $this->_nextValue <ins> $this->*allocationSize; $updateSql = $conn->getDatabasePlatform()->getTableHiLoUpdateNextValSql( $this->*tableName, $this->_sequenceName, $this->*allocationSize ); if ($conn->executeUpdate($updateSql, array(1 => $currentLevel, 2 => $currentLevel</ins>1)) !== 1) { // no affected rows, concurrency issue, throw exception } } else { // no current level returned, TableGenerator seems to be broken, throw exception } } else { // only table locks help here, implement this or throw exception? // or do we want to work with table locks exclusively? } } return $this->_nextValue<ins></ins>; } } ```
admin added the Improvement label 2026-01-22 12:42:38 +01:00
admin closed this issue 2026-01-22 12:42:39 +01:00
Author
Owner

@doctrinebot commented on GitHub (Mar 20, 2010):

@doctrinebot commented on GitHub (Mar 20, 2010): - depends on [DBAL-223: Add DBAL TableGenerator](http://www.doctrine-project.org/jira/browse/DBAL-223)
Author
Owner

@doctrinebot commented on GitHub (Aug 4, 2010):

Comment created by @guilhermeblanco:

Already merged into core.

@doctrinebot commented on GitHub (Aug 4, 2010): Comment created by @guilhermeblanco: Already merged into core.
Author
Owner

@doctrinebot commented on GitHub (Aug 4, 2010):

Comment created by @beberlei:

But it is not enabled yet :-) Plus we need tests to verify this works in high concurrency enviroments and does not pass the same id twice.

Furthermore the DAtabase Platform Methods are completly missing. No implementations yet.

@doctrinebot commented on GitHub (Aug 4, 2010): Comment created by @beberlei: But it is not enabled yet :-) Plus we need tests to verify this works in high concurrency enviroments and does not pass the same id twice. Furthermore the DAtabase Platform Methods are completly missing. No implementations yet.
Author
Owner

@doctrinebot commented on GitHub (Aug 4, 2010):

Comment created by @beberlei:

Schema-Tool support is also missing.

@doctrinebot commented on GitHub (Aug 4, 2010): Comment created by @beberlei: Schema-Tool support is also missing.
Author
Owner

@Majkl578 commented on GitHub (Sep 11, 2017):

Should be reconsidered for 3.0, either remove it entirely (it has never been enabled afaik) or finish & enable it.

@Majkl578 commented on GitHub (Sep 11, 2017): Should be reconsidered for 3.0, either remove it entirely (it has never been enabled afaik) or finish & enable it.
Author
Owner

@derrabus commented on GitHub (May 11, 2022):

Closed via #8961

@derrabus commented on GitHub (May 11, 2022): Closed via #8961
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#562