1
0
mirror of synced 2025-02-12 10:19:24 +03:00
doctrine2/lib/Doctrine/ORM/Id/SequenceGenerator.php

50 lines
1.3 KiB
PHP
Raw Normal View History

<?php
namespace Doctrine\ORM\Id;
use Doctrine\ORM\EntityManager;
class SequenceGenerator extends AbstractIdGenerator
2008-09-07 13:48:40 +00:00
{
private $_allocationSize;
2008-09-07 13:48:40 +00:00
private $_sequenceName;
private $_nextValue = 0;
private $_maxValue = null;
2008-09-07 13:48:40 +00:00
public function __construct(EntityManager $em, $sequenceName, $allocationSize = 20)
2008-09-07 13:48:40 +00:00
{
parent::__construct($em);
2008-09-07 13:48:40 +00:00
$this->_sequenceName = $sequenceName;
$this->_allocationSize = $allocationSize;
2008-09-07 13:48:40 +00:00
}
/**
* Generates an ID for the given entity.
2008-09-07 13:48:40 +00:00
*
* @param object $entity
* @return integer|float The generated value.
2008-09-07 13:48:40 +00:00
* @override
*/
public function generate($entity)
2008-09-07 13:48:40 +00:00
{
if ($this->_maxValue === null || $this->_nextValue == $this->_maxValue) {
// Allocate new values
$conn = $this->_em->getConnection();
$sql = $conn->getDatabasePlatform()->getSequenceNextValSql($this->_sequenceName);
$this->_maxValue = $conn->fetchOne($sql);
$this->_nextValue = $this->_maxValue - $this->_allocationSize;
}
return $this->_nextValue++;
}
public function getCurrentMaxValue()
{
return $this->_maxValue;
}
public function getNextValue()
{
return $this->_nextValue;
2008-09-07 13:48:40 +00:00
}
}