1
0
mirror of synced 2025-01-18 14:31:40 +03:00

Merge branch 'DDC-451'

This commit is contained in:
Benjamin Eberlei 2012-03-12 12:56:44 +01:00
commit 775936399e
5 changed files with 115 additions and 0 deletions

View File

@ -161,6 +161,7 @@
<xs:enumeration value="SEQUENCE"/> <xs:enumeration value="SEQUENCE"/>
<xs:enumeration value="IDENTITY"/> <xs:enumeration value="IDENTITY"/>
<xs:enumeration value="AUTO"/> <xs:enumeration value="AUTO"/>
<xs:enumeration value="UUID"/>
</xs:restriction> </xs:restriction>
</xs:simpleType> </xs:simpleType>

View File

@ -0,0 +1,48 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ORM\Id;
use Serializable, Doctrine\ORM\EntityManager;
/**
* Represents an ID generator that uses the database UUID expression
*
* @since 2.3
* @author Maarten de Keizer <m.de.keizer@markei.nl>
*/
class UuidGenerator extends AbstractIdGenerator
{
/**
* Generates an ID for the given entity.
*
* @param Doctrine\ORM\EntityManager $em The EntityManager to user
* @param object $entity
* @return string The generated value.
* @override
*/
public function generate(EntityManager $em, $entity)
{
$conn = $em->getConnection();
$sql = 'SELECT ' . $conn->getDatabasePlatform()->getGuidExpression();
return $conn->query($sql)->fetchColumn(0);
}
}

View File

@ -515,6 +515,9 @@ class ClassMetadataFactory implements ClassMetadataFactoryInterface
case ClassMetadata::GENERATOR_TYPE_NONE: case ClassMetadata::GENERATOR_TYPE_NONE:
$class->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator()); $class->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator());
break; break;
case ClassMetadata::GENERATOR_TYPE_UUID:
$class->setIdGenerator(new \Doctrine\ORM\Id\UuidGenerator());
break;
case ClassMetadata::GENERATOR_TYPE_TABLE: case ClassMetadata::GENERATOR_TYPE_TABLE:
throw new ORMException("TableGenerator not yet implemented."); throw new ORMException("TableGenerator not yet implemented.");
break; break;

View File

@ -94,6 +94,11 @@ class ClassMetadataInfo implements ClassMetadata
* must have a natural, manually assigned id. * must have a natural, manually assigned id.
*/ */
const GENERATOR_TYPE_NONE = 5; const GENERATOR_TYPE_NONE = 5;
/**
* UUID means that a UUID/GUID expression is used for id generation. Full
* portability is currently not guaranteed.
*/
const GENERATOR_TYPE_UUID = 6;
/** /**
* DEFERRED_IMPLICIT means that changes of entities are calculated at commit-time * DEFERRED_IMPLICIT means that changes of entities are calculated at commit-time
* by doing a property-by-property comparison with the original data. This will * by doing a property-by-property comparison with the original data. This will
@ -1559,6 +1564,16 @@ class ClassMetadataInfo implements ClassMetadata
{ {
return $this->generatorType == self::GENERATOR_TYPE_NONE; return $this->generatorType == self::GENERATOR_TYPE_NONE;
} }
/**
* Checks whether the class use a UUID for id generation
*
* @return boolean
*/
public function isIdentifierUuid()
{
return $this->generatorType == self::GENERATOR_TYPE_UUID;
}
/** /**
* Gets the type of a field. * Gets the type of a field.

View File

@ -0,0 +1,48 @@
<?php
namespace Doctrine\Tests\ORM\Functional;
/**
* @group DDC-451
*/
class UUIDGeneratorTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
public function setUp()
{
parent::setUp();
if ($this->_em->getConnection()->getDatabasePlatform()->getName() != 'mysql') {
$this->markTestSkipped('Currently restricted to MySQL platform.');
}
$this->_schemaTool->createSchema(array(
$this->_em->getClassMetadata(__NAMESPACE__ . '\\UUIDEntity')
));
}
public function testGenerateUUID()
{
$entity = new UUIDEntity();
$this->_em->persist($entity);
$this->assertNotNull($entity->getId());
$this->assertTrue(strlen($entity->getId()) > 0);
}
}
/**
* @Entity
*/
class UUIDEntity
{
/** @Id @Column(type="string") @GeneratedValue(strategy="UUID") */
private $id;
/**
* Get id.
*
* @return id.
*/
public function getId()
{
return $this->id;
}
}