Skip to content
Browse files

More work around Value Objects.

  • Loading branch information...
1 parent d519074 commit cc960bab8552c1f376bef6234822f6eb6a173855 @guilhermeblanco guilhermeblanco committed Jun 18, 2012
View
4 doctrine-mapping.xsd
@@ -95,7 +95,7 @@
<xs:element name="named-queries" type="orm:named-queries" minOccurs="0" maxOccurs="1" />
<xs:element name="id" type="orm:id" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="field" type="orm:field" minOccurs="0" maxOccurs="unbounded"/>
- <xs:element name="embed-one" type="orm:embed-one" minOccurs="0" maxOccurs="unbounded"/>
+ <xs:element name="embedded" type="orm:embedded" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="one-to-one" type="orm:one-to-one" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="one-to-many" type="orm:one-to-many" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="many-to-one" type="orm:many-to-one" minOccurs="0" maxOccurs="unbounded" />
@@ -192,7 +192,7 @@
<xs:anyAttribute namespace="##other"/>
</xs:complexType>
- <xs:complexType name="embed-one">
+ <xs:complexType name="embedded">
<xs:sequence>
<xs:any minOccurs="0" maxOccurs="unbounded" namespace="##other"/>
</xs:sequence>
View
2 lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
@@ -1950,7 +1950,7 @@ public function mapField(array $mapping)
*
* @param array $mapping The embedded mapping.
*/
- public function mapEmbedOne(array $mapping)
+ public function mapEmbedded(array $mapping)
{
$mapping = $this->_validateAndCompleteEmbeddedMapping($mapping);
$sourceFieldName = $mapping['fieldName'];
View
14 lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php
@@ -289,7 +289,7 @@ public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
}
// Field can only be annotated with one of:
- // @Column, @EmbedOne, @OneToOne, @OneToMany, @ManyToOne, @ManyToMany
+ // @Column, @Embedded, @OneToOne, @OneToMany, @ManyToOne, @ManyToMany
if ($columnAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Column')) {
if ($columnAnnot->type == null) {
throw MappingException::propertyTypeIsRequired($className, $property->getName());
@@ -338,14 +338,14 @@ public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
} else if ($tblGeneratorAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\TableGenerator')) {
throw MappingException::tableIdGeneratorNotImplemented($className);
}
- } else if ($embedOneAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\EmbedOne')) {
- $mapping['class'] = $embedOneAnnot->class;
+ } else if ($embeddedAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Embedded')) {
+ $mapping['class'] = $embeddedAnnot->class;
- if (isset($embedOneAnnot->prefix)) {
- $mapping['prefix'] = $embedOneAnnot->prefix;
+ if (isset($embeddedAnnot->prefix)) {
+ $mapping['prefix'] = $embeddedAnnot->prefix;
}
-
- $metadata->mapEmbedOne($mapping);
+
+ $metadata->mapEmbedded($mapping);
} else if ($oneToOneAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\OneToOne')) {
if ($idAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Id')) {
$mapping['id'] = true;
View
2 lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php
@@ -30,7 +30,7 @@
require_once __DIR__.'/../JoinColumn.php';
require_once __DIR__.'/../JoinColumns.php';
require_once __DIR__.'/../Column.php';
-require_once __DIR__.'/../EmbedOne.php';
+require_once __DIR__.'/../Embedded.php';
require_once __DIR__.'/../OneToOne.php';
require_once __DIR__.'/../OneToMany.php';
require_once __DIR__.'/../ManyToOne.php';
View
16 lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php
@@ -263,19 +263,19 @@ public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
}
}
- // Evaluate <embed-one ...> mappings
- if (isset($xmlRoot->{'embed-one'})) {
- foreach ($xmlRoot->{'embed-one'} as $embedOneMapping) {
+ // Evaluate <embedded ...> mappings
+ if (isset($xmlRoot->{'embedded'})) {
+ foreach ($xmlRoot->{'embedded'} as $embeddedMapping) {
$mapping = array(
- 'fieldName' => (string) $embedOneMapping['name'],
- 'class' => (string) $embedOneMapping['class'],
+ 'fieldName' => (string) $embeddedMapping['name'],
+ 'class' => (string) $embeddedMapping['class'],
);
- if (isset($embedOneMapping['prefix'])) {
- $mapping['prefix'] = (string) $embedOneMapping['prefix'];
+ if (isset($embeddedMapping['prefix'])) {
+ $mapping['prefix'] = (string) $embeddedMapping['prefix'];
}
- $metadata->mapEmbedOne($mapping);
+ $metadata->mapEmbedded($mapping);
}
}
View
14 lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php
@@ -261,19 +261,19 @@ public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
}
}
- // Evaluate embedOne fields
- if (isset($element['embedOne'])) {
- foreach ($element['embedOne'] as $name => $embedOneMapping) {
+ // Evaluate embedded fields
+ if (isset($element['embedded'])) {
+ foreach ($element['embedded'] as $name => $embeddedMapping) {
$mapping = array(
'fieldName' => $name,
- 'class' => $embedOneMapping['class'],
+ 'class' => $embeddedMapping['class'],
);
- if (isset($embedOneMapping['prefix'])) {
- $mapping['prefix'] = $embedOneMapping['prefix'];
+ if (isset($embeddedMapping['prefix'])) {
+ $mapping['prefix'] = $embeddedMapping['prefix'];
}
- $metadata->mapEmbedOne($mapping);
+ $metadata->mapEmbedded($mapping);
}
}
View
2 lib/Doctrine/ORM/Mapping/EmbedOne.php → lib/Doctrine/ORM/Mapping/Embedded.php
@@ -23,7 +23,7 @@
* @Annotation
* @Target("PROPERTY")
*/
-final class EmbedOne implements Annotation
+final class Embedded implements Annotation
{
/** @var string */
public $class;
View
71 lib/Doctrine/ORM/Persisters/BasicEntityPersister.php
@@ -1152,11 +1152,26 @@ protected function _getInsertSQL()
$this->_insertSql = $insertSql;
return $this->_insertSql;
+ } else {
+ $columns = array_unique($columns);
+ $values = array();
+
+ foreach ($columns as $column) {
+ $placeholder = '?';
+
+ if (isset($this->_columnTypes[$column]) &&
+ isset($this->_class->fieldMappings[$this->_class->fieldNames[$column]]['requireSQLConversion'])) {
+ $type = Type::getType($this->_columnTypes[$column]);
+ $placeholder = $type->convertToDatabaseValueSQL('?', $this->_platform);
+ }
+
+ $values[] = $placeholder;
+ }
}
$insertSql = 'INSERT INTO ' . $this->_class->getQuotedTableName($this->_platform)
- . ' (' . implode(', ', array_keys($columns)) . ') VALUES'
- . ' (' . implode(', ', array_values($columns)) . ')';
+ . ' (' . implode(', ', array_values($columns)) . ') VALUES'
+ . ' (' . implode(', ', $values) . ')';
$this->_insertSql = $insertSql;
@@ -1173,9 +1188,7 @@ protected function _getInsertSQL()
*/
protected function _getInsertColumnList()
{
- $columns = array();
-
- return $this->_getClassMetadataInsertColumnList($this->_class, $columns);
+ return $this->_getClassMetadataInsertColumnList($this->_class, array());
}
/**
@@ -1193,28 +1206,32 @@ private function _getClassMetadataInsertColumnList($class, $columns)
continue;
}
+ $newColumns = array();
+
switch (true) {
case (isset($class->associationMappings[$name])):
- $columns = $this->_getInsertAssociationColumnList(
- $class, $class->associationMappings[$name], $columns
+ $newColumns = $this->_getInsertAssociationColumnList(
+ $class, $class->associationMappings[$name]
);
break;
case (isset($class->embeddedMappings[$name])):
- $columns = $this->_getInsertEmbeddedColumnList(
- $class, $class->embeddedMappings[$name], $columns
+ $newColumns = $this->_getInsertEmbeddedColumnList(
+ $class, $class->embeddedMappings[$name]
);
break;
case ($class->generatorType != ClassMetadata::GENERATOR_TYPE_IDENTITY || $class->identifier[0] != $name):
- $columns = $this->_getInsertFieldColumnList(
- $class, $class->fieldMappings[$name], $columns
+ $newColumns = $this->_getInsertFieldColumnList(
+ $class, $class->fieldMappings[$name]
);
break;
default:
// Do nothing
}
+
+ $columns = array_merge($columns, $newColumns);
}
return $columns;
@@ -1225,30 +1242,37 @@ private function _getClassMetadataInsertColumnList($class, $columns)
*
* @param \Doctrine\ORM\Mapping\ClassMetadata $class
* @param array $mapping
- * @param array $columns
* @return array
*/
- private function _getInsertEmbeddedColumnList($class, $mapping, $columns)
+ private function _getInsertEmbeddedColumnList($class, $mapping)
{
$embedded = $class->embeddedMappings[$mapping['fieldName']];
$embeddable = $this->_em->getClassMetadata($embedded['class']);
- return $this->_getClassMetadataInsertColumnList($embeddable, $columns);
+ $columns = array();
+ $embeddedColumns = $this->_getClassMetadataInsertColumnList($embeddable, array());
+
+ foreach ($embeddedColumns as $embeddedColumnName => $embeddedColumnValue) {
+ $columns[] = $embedded['prefix'] . '_' . $embeddedColumnName;
+ }
+
+ return $columns;
}
/**
* Gets the list of columns to put in the INSERT SQL statement of an association.
*
* @param \Doctrine\ORM\Mapping\ClassMetadata $class
* @param array $mapping
- * @param array $columns
* @return array
*/
- private function _getInsertAssociationColumnList($class, $mapping, $columns)
+ private function _getInsertAssociationColumnList($class, $mapping)
{
+ $columns = array();
+
if ($mapping['isOwningSide'] && $mapping['type'] & ClassMetadata::TO_ONE) {
foreach ($mapping['targetToSourceKeyColumns'] as $sourceCol) {
- $columns[$sourceCol] = '?';
+ $columns[] = $sourceCol;
}
}
@@ -1260,21 +1284,16 @@ private function _getInsertAssociationColumnList($class, $mapping, $columns)
*
* @param \Doctrine\ORM\Mapping\ClassMetadata $class
* @param array $mapping
- * @param array $columns
* @return array
*/
- private function _getInsertFieldColumnList($class, $mapping, $columns)
+ private function _getInsertFieldColumnList($class, $mapping)
{
+ $columns = array();
+
$name = $mapping['fieldName'];
$quotedName = $class->getQuotedColumnName($name, $this->_platform);
- $placeholder = '?';
-
- if (isset($mapping['requireSQLConversion'])) {
- $type = Type::getType($mapping['type']);
- $placeholder = $type->convertToDatabaseValueSQL($placeholder, $this->_platform);
- }
- $columns[$quotedName] = $placeholder;
+ $columns[] = $quotedName;
$this->_columnTypes[$name] = $mapping['type'];
View
7 lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php
@@ -463,6 +463,13 @@ protected function _getInsertColumnList()
$columns[] = $sourceCol;
}
}
+ } else if (isset($this->_class->embeddedMappings[$name])) {
+ $embedded = $this->_class->embeddedMappings[$name];
+ $embeddedClass = $this->_em->getClassMetadata($embedded['class']);
+
+ foreach ($embeddedClass->fieldMappings as $embeddedFieldName => $embeddedFieldMapping) {
+ $columns[] = $embeddedClass->getQuotedColumnName($embeddedFieldName, $this->_platform);
+ }
} else if ($this->_class->name != $this->_class->rootEntityName ||
! $this->_class->isIdGeneratorIdentity() || $this->_class->identifier[0] != $name) {
$columns[] = $this->_class->getQuotedColumnName($name, $this->_platform);
View
36 lib/Doctrine/ORM/Tools/SchemaTool.php
@@ -143,6 +143,7 @@ public function getSchemaFromMetadata(array $classes)
if ($class->isInheritanceTypeSingleTable()) {
$columns = $this->_gatherColumns($class, $table);
$this->_gatherRelationsSql($class, $table, $schema);
+ $this->_gatherEmbeddedsSql($class, $table, $schema);
// Add the discriminator column
$this->addDiscriminatorColumnDefinition($class, $table);
@@ -174,6 +175,7 @@ public function getSchemaFromMetadata(array $classes)
}
$this->_gatherRelationsSql($class, $table, $schema);
+ $this->_gatherEmbeddedsSql($class, $table, $schema);
// Add the discriminator column only to the root table
if ($class->name == $class->rootEntityName) {
@@ -203,6 +205,7 @@ public function getSchemaFromMetadata(array $classes)
} else {
$this->_gatherColumns($class, $table);
$this->_gatherRelationsSql($class, $table, $schema);
+ $this->_gatherEmbeddedsSql($class, $table, $schema);
}
$pkColumns = array();
@@ -306,6 +309,8 @@ private function _gatherColumns($class, $table)
if ($class->isIdentifier($mapping['fieldName'])) {
$pkColumns[] = $class->getQuotedColumnName($mapping['fieldName'], $this->_platform);
}
+
+ $columns[] = $column;
}
// For now, this is a hack required for single table inheritence, since this method is called
@@ -437,6 +442,37 @@ private function _gatherRelationsSql($class, $table, $schema)
}
/**
+ * Gathers the SQL for properly setting up the embeddeds of the given class.
+ *
+ * @param ClassMetadata $class
+ * @param \Doctrine\DBAL\Schema\Table $table
+ * @param \Doctrine\DBAL\Schema\Schema $schema
+ * @return void
+ */
+ private function _gatherEmbeddedsSql($class, $table, $schema)
+ {
+ foreach ($class->embeddedMappings as $embeddedFieldMapping) {
+ \Doctrine\Common\Util\Debug::dump($embeddedFieldMapping, 6);
+
+ if (isset($embeddedFieldMapping['inherited'])) {
+ continue;
+ }
+
+ $embeddedClass = $this->_em->getClassMetadata($embeddedFieldMapping['class']);
+
+ // Map each individual field in an optimized way
+ foreach ($embeddedClass->fieldMappings as $fieldName => $mapping) {
+ // Override fieldName for prefix generation
+ $mapping['fieldName'] = $embeddedFieldMapping['prefix'] . '_' . $mapping['fieldName'];
+
+ $this->_gatherColumn($embeddedClass, $mapping, $table);
+ }
+
+ \Doctrine\Common\Util\Debug::dump($table, 6);
+ }
+ }
+
+ /**
* Get the class metadata that is responsible for the definition of the referenced column name.
*
* Previously this was a simple task, but with DDC-117 this problem is actually recursive. If its
2 lib/vendor/doctrine-dbal
@@ -1 +1 @@
-Subproject commit 29b714b7fe72641d749ae90324a5759853fe09b0
+Subproject commit b8d8d9535472ea805f14187343f80ac6cb3c2614
View
2 tests/Doctrine/Tests/Models/CMS/CmsUser.php
@@ -31,7 +31,7 @@ class CmsUser
*/
public $name;
/**
- * @EmbedOne(class="CmsParents", prefix="parents")
+ * @Embedded(class="CmsParents", prefix="parents")
*/
public $parents;
/**
View
8 tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php
@@ -149,7 +149,7 @@ public function testIdentifier($class)
* @depends testIdentifier
* @param ClassMetadata $class
*/
- public function testEmbedOnes($class)
+ public function testEmbeddeds($class)
{
$this->assertEquals(1, count($class->embeddedMappings));
@@ -160,7 +160,7 @@ public function testEmbedOnes($class)
}
/**
- * @depends testEmbedOnes
+ * @depends testEmbeddeds
* @param ClassMetadata $class
*/
public function testAssocations($class)
@@ -462,7 +462,7 @@ class User
public $email;
/**
- * @EmbedOne(class="Parents")
+ * @Embedded(class="Parents")
*/
public $parents;
@@ -539,7 +539,7 @@ public static function loadMetadata(ClassMetadataInfo $metadata)
'columnName' => 'user_email',
'columnDefinition' => 'CHAR(32) NOT NULL',
));
- $metadata->mapEmbedOne(array(
+ $metadata->mapEmbedded(array(
'fieldName' => 'parents',
'class' => 'Parents',
));
View
2 tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.User.php
@@ -34,7 +34,7 @@
'columnName' => 'user_email',
'columnDefinition' => 'CHAR(32) NOT NULL',
));
-$metadata->mapEmbedOne(array(
+$metadata->mapEmbedded(array(
'fieldName' => 'parents',
'class' => 'Parents',
));
View
2 tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.User.dcm.xml
@@ -34,7 +34,7 @@
<field name="name" column="name" type="string" length="50" nullable="true" unique="true" />
<field name="email" column="user_email" type="string" column-definition="CHAR(32) NOT NULL" />
- <embed-one name="parents" class="Parents" />
+ <embedded name="parents" class="Parents" />
<one-to-one field="address" target-entity="Address" inversed-by="user">
<cascade><cascade-remove /></cascade>
View
2 tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.ORM.Mapping.User.dcm.yml
@@ -22,7 +22,7 @@ Doctrine\Tests\ORM\Mapping\User:
type: string
column: user_email
columnDefinition: CHAR(32) NOT NULL
- embedOne:
+ embedded:
parents:
class: Parents
oneToOne:

0 comments on commit cc960ba

Please sign in to comment.
Something went wrong with that request. Please try again.