diff --git a/UPGRADE_TO_2_0 b/UPGRADE_TO_2_0 index 66e00e0ab..ebbdcb0b9 100644 --- a/UPGRADE_TO_2_0 +++ b/UPGRADE_TO_2_0 @@ -1,3 +1,10 @@ +# Update from 2.0-BETA2 to 2.0-BETA3 + +## Default Allocation Size for Sequences + +The default allocation size for sequences has been changed from 10 to 1. This step was made +to not cause confusion with users and also because it is partly some kind of premature optimization. + # Update from 2.0-BETA1 to 2.0-BETA2 There are no backwards incompatible changes in this release. diff --git a/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php b/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php index cb3a7f1cd..99b4fa77d 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php @@ -1,7 +1,5 @@ * @author Jonathan Wage + * @author Benjamin Eberlei */ class DatabaseDriver implements Driver { @@ -73,6 +71,11 @@ class DatabaseDriver implements Driver $foreignKeys = array(); } + $allForeignKeyColumns = array(); + foreach ($foreignKeys AS $foreignKey) { + $allForeignKeyColumns = array_merge($allForeignKeyColumns, $foreignKey->getLocalColumns()); + } + $indexes = $this->_sm->listTableIndexes($tableName); $ids = array(); @@ -81,6 +84,8 @@ class DatabaseDriver implements Driver $fieldMapping = array(); if (isset($indexes['primary']) && in_array($column->getName(), $indexes['primary']->getColumns())) { $fieldMapping['id'] = true; + } else if (in_array($column->getName(), $allForeignKeyColumns)) { + continue; } $fieldMapping['fieldName'] = Inflector::camelize(strtolower($column->getName())); @@ -123,7 +128,7 @@ class DatabaseDriver implements Driver $fkCols = $foreignKey->getForeignColumns(); $associationMapping = array(); - $associationMapping['fieldName'] = Inflector::camelize(str_ireplace('_id', '', $localColumn)); + $associationMapping['fieldName'] = Inflector::camelize(str_replace('_id', '', strtolower($localColumn))); $associationMapping['targetEntity'] = Inflector::classify($foreignKey->getForeignTableName()); for ($i = 0; $i < count($cols); $i++) { @@ -146,16 +151,19 @@ class DatabaseDriver implements Driver } /** - * {@inheritDoc} + * Return all the class names supported by this driver. + * + * IMPORTANT: This method must return an array of table names because we need + * to know the table name after we inflect it to create the entity class name. + * + * @return array */ public function getAllClassNames() { $classes = array(); - foreach ($this->_sm->listTables() as $table) { - // This method must return an array of table names because we need - // to know the table name after we inflect it to create the entity class name. - $classes[] = $table->getName(); + foreach ($this->_sm->listTableNames() as $tableName) { + $classes[] = $tableName; } return $classes; diff --git a/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php b/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php index 537aaf3a3..fb7871c74 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php +++ b/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php @@ -119,7 +119,7 @@ final class JoinTable extends Annotation { } final class SequenceGenerator extends Annotation { public $sequenceName; - public $allocationSize = 10; + public $allocationSize = 1; public $initialValue = 1; } final class ChangeTrackingPolicy extends Annotation {} diff --git a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php index b038973a5..3d90cd122 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php @@ -106,21 +106,25 @@ class XmlDriver extends AbstractFileDriver // Evaluate if (isset($xmlRoot->indexes)) { + $metadata->table['indexes'] = array(); foreach ($xmlRoot->indexes->index as $index) { - if (is_string($index['columns'])) { - $columns = explode(',', $index['columns']); - } else { - $columns = $index['columns']; - } + $columns = explode(',', (string)$index['columns']); - $metadata->table['indexes'][$index['name']] = array( - 'columns' => $columns - ); + if (isset($index['name'])) { + $metadata->table['indexes'][(string)$index['name']] = array( + 'columns' => $columns + ); + } else { + $metadata->table['indexes'][] = array( + 'columns' => $columns + ); + } } } // Evaluate if (isset($xmlRoot->{'unique-constraints'})) { + $metadata->table['uniqueConstraints'] = array(); foreach ($xmlRoot->{'unique-constraints'}->{'unique-constraint'} as $unique) { $columns = explode(',', (string)$unique['columns']); diff --git a/tests/Doctrine/Tests/Models/Generic/DateTimeModel.php b/tests/Doctrine/Tests/Models/Generic/DateTimeModel.php index 0733245ff..3298a8850 100644 --- a/tests/Doctrine/Tests/Models/Generic/DateTimeModel.php +++ b/tests/Doctrine/Tests/Models/Generic/DateTimeModel.php @@ -14,15 +14,15 @@ class DateTimeModel */ public $id; /** - * @Column(name="col_datetime", type="datetime") + * @Column(name="col_datetime", type="datetime", nullable=true) */ public $datetime; /** - * @Column(name="col_date", type="date") + * @Column(name="col_date", type="date", nullable=true) */ public $date; /** - * @Column(name="col_time", type="time") + * @Column(name="col_time", type="time", nullable=true) */ public $time; } \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Functional/DatabaseDriverTest.php b/tests/Doctrine/Tests/ORM/Functional/DatabaseDriverTest.php index c11522b28..28488f6a9 100644 --- a/tests/Doctrine/Tests/ORM/Functional/DatabaseDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/DatabaseDriverTest.php @@ -21,7 +21,7 @@ class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_sm = $this->_em->getConnection()->getSchemaManager(); } - public function testCreateSimpleYamlFromDatabase() + public function testLoadMetadataFromDatabase() { if (!$this->_em->getConnection()->getDatabasePlatform()->supportsForeignKeyConstraints()) { $this->markTestSkipped('Platform does not support foreign keys.'); @@ -34,7 +34,10 @@ class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_sm->dropAndCreateTable($table); - $metadata = $this->extractClassMetadata("DbdriverFoo"); + $metadatas = $this->extractClassMetadata(array("DbdriverFoo")); + + $this->assertArrayHasKey('dbdriver_foo', $metadatas); + $metadata = $metadatas['dbdriver_foo']; $this->assertArrayHasKey('id', $metadata->fieldMappings); $this->assertEquals('id', $metadata->fieldMappings['id']['fieldName']); @@ -49,7 +52,7 @@ class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertTrue($metadata->fieldMappings['bar']['nullable']); } - public function testCreateYamlWithForeignKeyFromDatabase() + public function testLoadMetadataWithForeignKeyFromDatabase() { if (!$this->_em->getConnection()->getDatabasePlatform()->supportsForeignKeyConstraints()) { $this->markTestSkipped('Platform does not support foreign keys.'); @@ -69,15 +72,18 @@ class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_sm->dropAndCreateTable($tableA); - $metadata = $this->extractClassMetadata("DbdriverBaz"); + $metadatas = $this->extractClassMetadata(array("DbdriverBar", "DbdriverBaz")); - $this->assertArrayNotHasKey('bar', $metadata->fieldMappings); - $this->assertArrayHasKey('id', $metadata->fieldMappings); + $this->assertArrayHasKey('dbdriver_baz', $metadatas); + $bazMetadata = $metadatas['dbdriver_baz']; - $metadata->associationMappings = \array_change_key_case($metadata->associationMappings, \CASE_LOWER); + $this->assertArrayNotHasKey('barId', $bazMetadata->fieldMappings, "The foreign Key field should not be inflected as 'barId' field, its an association."); + $this->assertArrayHasKey('id', $bazMetadata->fieldMappings); - $this->assertArrayHasKey('bar', $metadata->associationMappings); - $this->assertType('Doctrine\ORM\Mapping\OneToOneMapping', $metadata->associationMappings['bar']); + $bazMetadata->associationMappings = \array_change_key_case($bazMetadata->associationMappings, \CASE_LOWER); + + $this->assertArrayHasKey('bar', $bazMetadata->associationMappings); + $this->assertType('Doctrine\ORM\Mapping\OneToOneMapping', $bazMetadata->associationMappings['bar']); } /** @@ -85,18 +91,24 @@ class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase * @param string $className * @return ClassMetadata */ - protected function extractClassMetadata($className) + protected function extractClassMetadata(array $classNames) { + $classNames = array_map('strtolower', $classNames); + $metadatas = array(); + $driver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver($this->_sm); foreach ($driver->getAllClassNames() as $dbTableName) { - if (strtolower(Inflector::classify($dbTableName)) != strtolower($className)) { + if (!in_array(strtolower(Inflector::classify($dbTableName)), $classNames)) { continue; } $class = new ClassMetadataInfo($dbTableName); $driver->loadMetadataForClass($dbTableName, $class); - return $class; + $metadatas[strtolower($dbTableName)] = $class; } - $this->fail("No class matching the name '".$className."' was found!"); + if (count($metadatas) != count($classNames)) { + $this->fail("Have not found all classes matching the names '" . implode(", ", $classNames) . "' only tables " . implode(", ", array_keys($metadatas))); + } + return $metadatas; } } diff --git a/tests/Doctrine/Tests/ORM/Functional/TypeTest.php b/tests/Doctrine/Tests/ORM/Functional/TypeTest.php index 001087639..a4ef84589 100644 --- a/tests/Doctrine/Tests/ORM/Functional/TypeTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/TypeTest.php @@ -91,4 +91,49 @@ class TypeTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertType('stdClass', $serialize->object); } + + public function testDate() + { + $dateTime = new DateTimeModel(); + $dateTime->date = new \DateTime('2009-10-01', new \DateTimeZone('Europe/Berlin')); + + $this->_em->persist($dateTime); + $this->_em->flush(); + $this->_em->clear(); + + $dateTimeDb = $this->_em->find('Doctrine\Tests\Models\Generic\DateTimeModel', $dateTime->id); + + $this->assertType('DateTime', $dateTimeDb->date); + $this->assertEquals('2009-10-01', $dateTimeDb->date->format('Y-m-d')); + } + + public function testDateTime() + { + $dateTime = new DateTimeModel(); + $dateTime->datetime = new \DateTime('2009-10-02 20:10:52', new \DateTimeZone('Europe/Berlin')); + + $this->_em->persist($dateTime); + $this->_em->flush(); + $this->_em->clear(); + + $dateTimeDb = $this->_em->find('Doctrine\Tests\Models\Generic\DateTimeModel', $dateTime->id); + + $this->assertType('DateTime', $dateTime->datetime); + $this->assertEquals('2009-10-02 20:10:52', $dateTimeDb->datetime->format('Y-m-d H:i:s')); + } + + public function testTime() + { + $dateTime = new DateTimeModel(); + $dateTime->time = new \DateTime('2010-01-01 19:27:20'); + + $this->_em->persist($dateTime); + $this->_em->flush(); + $this->_em->clear(); + + $dateTimeDb = $this->_em->find('Doctrine\Tests\Models\Generic\DateTimeModel', $dateTime->id); + + $this->assertType('DateTime', $dateTime->time); + $this->assertEquals('19:27:20', $dateTime->time->format('H:i:s')); + } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php index 270e16234..858a00eba 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php @@ -36,6 +36,21 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase return $class; } + /** + * @depends testEntityTableNameAndInheritance + * @param ClassMetadata $class + */ + public function testEntityIndexes($class) + { + $this->assertArrayHasKey('indexes', $class->table, 'ClassMetadata should have indexes key in table property.'); + $this->assertEquals(array( + 'name_idx' => array('columns' => array('name')), + 0 => array('columns' => array('user_email')) + ), $class->table['indexes']); + + return $class; + } + /** * @depends testEntityTableNameAndInheritance * @param ClassMetadata $class @@ -240,7 +255,11 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase /** * @Entity * @HasLifecycleCallbacks - * @Table(name="cms_users", uniqueConstraints={@UniqueConstraint(name="search_idx", columns={"name", "user_email"})}) + * @Table( + * name="cms_users", + * uniqueConstraints={@UniqueConstraint(name="search_idx", columns={"name", "user_email"})}, + * indexes={@Index(name="name_idx", columns={"name"}), @Index(name="0", columns={"user_email"})} + * ) */ class User { @@ -411,6 +430,9 @@ class User $metadata->table['uniqueConstraints'] = array( 'search_idx' => array('columns' => array('name', 'user_email')), ); + $metadata->table['indexes'] = array( + 'name_idx' => array('columns' => array('name')), 0 => array('columns' => array('user_email')) + ); $metadata->setSequenceGeneratorDefinition(array( 'sequenceName' => 'tablename_seq', 'allocationSize' => 100, diff --git a/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.User.php b/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.User.php index 819a01109..4aadffb30 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.User.php +++ b/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.User.php @@ -106,6 +106,9 @@ $metadata->mapManyToMany(array( $metadata->table['uniqueConstraints'] = array( 'search_idx' => array('columns' => array('name', 'user_email')), ); +$metadata->table['indexes'] = array( + 'name_idx' => array('columns' => array('name')), 0 => array('columns' => array('user_email')) +); $metadata->setSequenceGeneratorDefinition(array( 'sequenceName' => 'tablename_seq', 'allocationSize' => 100, diff --git a/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.User.dcm.xml b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.User.dcm.xml index be905e470..948430c24 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.User.dcm.xml +++ b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.User.dcm.xml @@ -7,6 +7,11 @@ + + + + + diff --git a/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.User.dcm.yml b/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.User.dcm.yml index 7dd6bfaed..b541c8877 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.User.dcm.yml +++ b/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.User.dcm.yml @@ -58,4 +58,9 @@ Doctrine\Tests\ORM\Mapping\User: postPersist: [ doStuffOnPostPersist ] uniqueConstraints: search_idx: - columns: name,user_email \ No newline at end of file + columns: name,user_email + indexes: + name_idx: + columns: name + 0: + columns: user_email \ No newline at end of file