Skip to content

Commit

Permalink
Merge pull request #978 from AntonStoeckl/embedded-empty-prefix
Browse files Browse the repository at this point in the history
[DDC-2987] Enable empty prefixes for inlined embeddable
  • Loading branch information
guilhermeblanco committed Mar 16, 2014
2 parents 44c1dae + 83ef47c commit 383604d
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 4 deletions.
15 changes: 12 additions & 3 deletions lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3158,6 +3158,7 @@ public function getMetadataValue($name) {
* Map Embedded Class
*
* @param array $mapping
* @throws MappingException
* @return void
*/
public function mapEmbedded(array $mapping)
Expand Down Expand Up @@ -3187,9 +3188,17 @@ public function inlineEmbeddable($property, ClassMetadataInfo $embeddable)
$fieldMapping['originalField'] = $fieldMapping['fieldName'];
$fieldMapping['fieldName'] = $property . "." . $fieldMapping['fieldName'];

$fieldMapping['columnName'] = ! empty($this->embeddedClasses[$property]['columnPrefix'])
? $this->embeddedClasses[$property]['columnPrefix'] . $fieldMapping['columnName']
: $this->namingStrategy->embeddedFieldToColumnName($property, $fieldMapping['columnName'], $this->reflClass->name, $embeddable->reflClass->name);
if (! empty($this->embeddedClasses[$property]['columnPrefix'])) {
$fieldMapping['columnName'] = $this->embeddedClasses[$property]['columnPrefix'] . $fieldMapping['columnName'];
} elseif ($this->embeddedClasses[$property]['columnPrefix'] !== false) {
$fieldMapping['columnName'] = $this->namingStrategy
->embeddedFieldToColumnName(
$property,
$fieldMapping['columnName'],
$this->reflClass->name,
$embeddable->reflClass->name
);
}

$this->mapField($fieldMapping);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Mapping/Embedded.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class Embedded implements Annotation
public $class;

/**
* @var string
* @var mixed
*/
public $columnPrefix;
}
100 changes: 100 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/ValueObjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,42 @@ public function testEmbeddableWithinEmbeddable()
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC93PhoneNumber')
));
}

public function testInlineEmbeddableWithPrefix()
{
$expectedColumnName = 'foobar_id';

$actualColumnName = $this->_em
->getClassMetadata(__NAMESPACE__ . '\DDC3028PersonWithPrefix')
->getColumnName('id.id');

$this->assertEquals($expectedColumnName, $actualColumnName);
}

public function testInlineEmbeddableEmptyPrefix()
{
$expectedColumnName = 'id_id';

$actualColumnName = $this->_em
->getClassMetadata(__NAMESPACE__ . '\DDC3028PersonEmptyPrefix')
->getColumnName('id.id');

$this->assertEquals($expectedColumnName, $actualColumnName);
}

public function testInlineEmbeddablePrefixFalse()
{
$expectedColumnName = 'id';

$actualColumnName = $this->_em
->getClassMetadata(__NAMESPACE__ . '\DDC3028PersonPrefixFalse')
->getColumnName('id.id');

$this->assertEquals($expectedColumnName, $actualColumnName);
}
}


/**
* @Entity
*/
Expand Down Expand Up @@ -296,3 +330,69 @@ class DDC93ContactInfo
/** @Embedded(class = "DDC93Address") */
private $address;
}

/**
* @Entity
*/
class DDC3028PersonWithPrefix
{
const CLASSNAME = __CLASS__;

/** @Embedded(class="DDC3028Id", columnPrefix = "foobar_") */
public $id;

public function __construct(DDC3028Id $id = null)
{
$this->id = $id;
}
}

/**
* @Entity
*/
class DDC3028PersonEmptyPrefix
{
const CLASSNAME = __CLASS__;

/** @Embedded(class="DDC3028Id", columnPrefix = "") */
public $id;

public function __construct(DDC3028Id $id = null)
{
$this->id = $id;
}
}

/**
* @Entity
*/
class DDC3028PersonPrefixFalse
{
const CLASSNAME = __CLASS__;

/** @Embedded(class="DDC3028Id", columnPrefix = false) */
public $id;

public function __construct(DDC3028Id $id = null)
{
$this->id = $id;
}
}

/**
* @Embeddable
*/
class DDC3028Id
{
const CLASSNAME = __CLASS__;

/**
* @Id @Column(type="string")
*/
public $id;

public function __construct($id = null)
{
$this->id = $id;
}
}

0 comments on commit 383604d

Please sign in to comment.