[2.0] Updating YAML and XML drivers to be synchronized with Annotations driver.
This commit is contained in:
parent
27356225dd
commit
fd89892cc9
@ -33,7 +33,7 @@
|
|||||||
</xs:sequence>
|
</xs:sequence>
|
||||||
</xs:complexType>
|
</xs:complexType>
|
||||||
|
|
||||||
<xs:simpleType name="lifecycle-callback-type">
|
<xs:simpleType name="lifecycle-listener-type">
|
||||||
<xs:restriction base="xs:token">
|
<xs:restriction base="xs:token">
|
||||||
<xs:enumeration value="prePersist"/>
|
<xs:enumeration value="prePersist"/>
|
||||||
<xs:enumeration value="postPersist"/>
|
<xs:enumeration value="postPersist"/>
|
||||||
@ -45,14 +45,14 @@
|
|||||||
</xs:restriction>
|
</xs:restriction>
|
||||||
</xs:simpleType>
|
</xs:simpleType>
|
||||||
|
|
||||||
<xs:complexType name="lifecycle-callback">
|
<xs:complexType name="lifecycle-listener">
|
||||||
<xs:attribute name="type" type="orm:lifecycle-callback-type" use="required" />
|
<xs:attribute name="type" type="orm:lifecycle-listener-type" use="required" />
|
||||||
<xs:attribute name="method" type="xs:NMTOKEN" use="required" />
|
<xs:attribute name="method" type="xs:NMTOKEN" use="required" />
|
||||||
</xs:complexType>
|
</xs:complexType>
|
||||||
|
|
||||||
<xs:complexType name="lifecycle-callbacks">
|
<xs:complexType name="lifecycle-listener">
|
||||||
<xs:sequence>
|
<xs:sequence>
|
||||||
<xs:element name="lifecycle-callback" type="orm:lifecycle-callback" minOccurs="1" maxOccurs="unbounded" />
|
<xs:element name="lifecycle-listener" type="orm:lifecycle-listener" minOccurs="1" maxOccurs="unbounded" />
|
||||||
</xs:sequence>
|
</xs:sequence>
|
||||||
</xs:complexType>
|
</xs:complexType>
|
||||||
|
|
||||||
|
@ -112,12 +112,6 @@ class AnnotationDriver implements Driver
|
|||||||
$metadata->setDiscriminatorMap($discrMapAnnot->value);
|
$metadata->setDiscriminatorMap($discrMapAnnot->value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Evaluate DoctrineSubClasses annotation
|
|
||||||
if (isset($classAnnotations['Doctrine\ORM\Mapping\SubClasses'])) {
|
|
||||||
$subClassesAnnot = $classAnnotations['Doctrine\ORM\Mapping\SubClasses'];
|
|
||||||
$metadata->setSubclasses($subClassesAnnot->value);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Evaluate DoctrineChangeTrackingPolicy annotation
|
// Evaluate DoctrineChangeTrackingPolicy annotation
|
||||||
if (isset($classAnnotations['Doctrine\ORM\Mapping\ChangeTrackingPolicy'])) {
|
if (isset($classAnnotations['Doctrine\ORM\Mapping\ChangeTrackingPolicy'])) {
|
||||||
$changeTrackingAnnot = $classAnnotations['Doctrine\ORM\Mapping\ChangeTrackingPolicy'];
|
$changeTrackingAnnot = $classAnnotations['Doctrine\ORM\Mapping\ChangeTrackingPolicy'];
|
||||||
|
@ -41,9 +41,19 @@ class XmlDriver extends AbstractFileDriver
|
|||||||
*/
|
*/
|
||||||
public function loadMetadataForClass($className, ClassMetadata $metadata)
|
public function loadMetadataForClass($className, ClassMetadata $metadata)
|
||||||
{
|
{
|
||||||
|
$class = $metadata->getReflectionClass();
|
||||||
|
|
||||||
$xmlRoot = $this->getElement($className);
|
$xmlRoot = $this->getElement($className);
|
||||||
|
|
||||||
if ($xmlRoot->getName() == 'entity') {
|
if ($xmlRoot->getName() == 'entity') {
|
||||||
|
$metadata->setCustomRepositoryClass(
|
||||||
|
isset($xmlRoot['repository-class']) ? $xmlRoot['repository-class'] : null
|
||||||
|
);
|
||||||
|
} else if ($xmlRoot->getName() == 'mapped-superclass') {
|
||||||
|
$metadata->isMappedSuperclass = true;
|
||||||
|
} else {
|
||||||
|
throw DoctrineException::updateMe("$className is no entity or mapped superclass.");
|
||||||
|
}
|
||||||
|
|
||||||
// Evaluate <entity...> attributes
|
// Evaluate <entity...> attributes
|
||||||
if (isset($xmlRoot['table'])) {
|
if (isset($xmlRoot['table'])) {
|
||||||
@ -56,6 +66,26 @@ class XmlDriver extends AbstractFileDriver
|
|||||||
$metadata->setInheritanceType((string)$xmlRoot['inheritance-type']);
|
$metadata->setInheritanceType((string)$xmlRoot['inheritance-type']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Evaluate <discriminator-column...>
|
||||||
|
if (isset($xmlRoot->{'discriminator-column'})) {
|
||||||
|
$discrColumn = $xmlRoot->{'discriminator-column'};
|
||||||
|
$metadata->setDiscriminatorColumn(array(
|
||||||
|
'name' => (string)$discrColumn->name,
|
||||||
|
'type' => (string)$discrColumn->type,
|
||||||
|
'length' => (string)$discrColumn->length
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Evaluate <discriminator-map...>
|
||||||
|
if (isset($xmlRoot->{'discriminator-map'})) {
|
||||||
|
$metadata->setDiscriminatorMap((array)$xmlRoot->{'discriminator-map'});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Evaluate <change-tracking-policy...>
|
||||||
|
if (isset($xmlRoot->{'change-tracking-policy'})) {
|
||||||
|
$metadata->setChangeTrackingPolicy((array)$xmlRoot->{'change-tracking-policy'});
|
||||||
|
}
|
||||||
|
|
||||||
// Evaluate <indexes...>
|
// Evaluate <indexes...>
|
||||||
if (isset($xmlRoot->indexes)) {
|
if (isset($xmlRoot->indexes)) {
|
||||||
foreach ($xmlRoot->indexes->index as $index) {
|
foreach ($xmlRoot->indexes->index as $index) {
|
||||||
@ -113,6 +143,18 @@ class XmlDriver extends AbstractFileDriver
|
|||||||
$metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_'
|
$metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_'
|
||||||
. (string)$idElement->generator['strategy']));
|
. (string)$idElement->generator['strategy']));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check for SequenceGenerator/TableGenerator definition
|
||||||
|
if (isset($idElement->{'sequence-generator'})) {
|
||||||
|
$seqGenerator = $idElement->{'sequence-generator'};
|
||||||
|
$metadata->setSequenceGeneratorDefinition(array(
|
||||||
|
'sequenceName' => $seqGenerator->{'sequence-name'},
|
||||||
|
'allocationSize' => $seqGenerator->{'allocation-size'},
|
||||||
|
'initialValue' => $seqGeneratorAnnot->{'initial-value'}
|
||||||
|
));
|
||||||
|
} else if (isset($idElement->{'table-generator'})) {
|
||||||
|
throw DoctrineException::tableIdGeneratorNotImplemented();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Evaluate <one-to-one ...> mappings
|
// Evaluate <one-to-one ...> mappings
|
||||||
@ -120,7 +162,7 @@ class XmlDriver extends AbstractFileDriver
|
|||||||
foreach ($xmlRoot->{'one-to-one'} as $oneToOneElement) {
|
foreach ($xmlRoot->{'one-to-one'} as $oneToOneElement) {
|
||||||
$mapping = array(
|
$mapping = array(
|
||||||
'fieldName' => (string)$oneToOneElement['field'],
|
'fieldName' => (string)$oneToOneElement['field'],
|
||||||
'targetEntity' => (string)$oneToOneElement['target-entity']
|
'targetEntity' => (string)$oneToOneElement['target-entity'],
|
||||||
);
|
);
|
||||||
if (isset($oneToOneElement['mapped-by'])) {
|
if (isset($oneToOneElement['mapped-by'])) {
|
||||||
$mapping['mappedBy'] = (string)$oneToOneElement['mapped-by'];
|
$mapping['mappedBy'] = (string)$oneToOneElement['mapped-by'];
|
||||||
@ -137,11 +179,12 @@ class XmlDriver extends AbstractFileDriver
|
|||||||
}
|
}
|
||||||
$mapping['joinColumns'] = $joinColumns;
|
$mapping['joinColumns'] = $joinColumns;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($oneToOneElement->cascade)) {
|
if (isset($oneToOneElement->cascade)) {
|
||||||
$mapping['cascade'] = $this->_getCascadeMappings($oneToOneElement->cascade);
|
$mapping['cascade'] = $this->_getCascadeMappings($oneToOneElement->cascade);
|
||||||
}
|
}
|
||||||
|
if (isset($oneToOneElement->{'orphan-removal'})) {
|
||||||
|
$mapping['orphanRemoval'] = (bool)$oneToOneElement->{'orphan-removal'};
|
||||||
|
}
|
||||||
$metadata->mapOneToOne($mapping);
|
$metadata->mapOneToOne($mapping);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -157,6 +200,9 @@ class XmlDriver extends AbstractFileDriver
|
|||||||
if (isset($oneToManyElement->cascade)) {
|
if (isset($oneToManyElement->cascade)) {
|
||||||
$mapping['cascade'] = $this->_getCascadeMappings($oneToManyElement->cascade);
|
$mapping['cascade'] = $this->_getCascadeMappings($oneToManyElement->cascade);
|
||||||
}
|
}
|
||||||
|
if (isset($oneToManyElement->{'orphan-removal'})) {
|
||||||
|
$mapping['orphanRemoval'] = (bool)$oneToManyElement->{'orphan-removal'};
|
||||||
|
}
|
||||||
$metadata->mapOneToMany($mapping);
|
$metadata->mapOneToMany($mapping);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -185,6 +231,9 @@ class XmlDriver extends AbstractFileDriver
|
|||||||
if (isset($manyToOneElement->cascade)) {
|
if (isset($manyToOneElement->cascade)) {
|
||||||
$mapping['cascade'] = $this->_getCascadeMappings($manyToOneElement->cascade);
|
$mapping['cascade'] = $this->_getCascadeMappings($manyToOneElement->cascade);
|
||||||
}
|
}
|
||||||
|
if (isset($manyToOneElement->{'orphan-removal'})) {
|
||||||
|
$mapping['orphanRemoval'] = (bool)$manyToOneElement->{'orphan-removal'};
|
||||||
|
}
|
||||||
$metadata->mapManyToOne($mapping);
|
$metadata->mapManyToOne($mapping);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -196,7 +245,6 @@ class XmlDriver extends AbstractFileDriver
|
|||||||
'fieldName' => (string)$manyToManyElement['field'],
|
'fieldName' => (string)$manyToManyElement['field'],
|
||||||
'targetEntity' => (string)$manyToManyElement['target-entity']
|
'targetEntity' => (string)$manyToManyElement['target-entity']
|
||||||
);
|
);
|
||||||
|
|
||||||
if (isset($manyToManyElement['mappedBy'])) {
|
if (isset($manyToManyElement['mappedBy'])) {
|
||||||
$mapping['mappedBy'] = (string)$manyToManyElement['mapped-by'];
|
$mapping['mappedBy'] = (string)$manyToManyElement['mapped-by'];
|
||||||
} else if (isset($manyToManyElement->{'join-table'})) {
|
} else if (isset($manyToManyElement->{'join-table'})) {
|
||||||
@ -217,17 +265,24 @@ class XmlDriver extends AbstractFileDriver
|
|||||||
} else {
|
} else {
|
||||||
throw MappingException::invalidMapping($mapping['fieldName']);
|
throw MappingException::invalidMapping($mapping['fieldName']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($manyToManyElement->cascade)) {
|
if (isset($manyToManyElement->cascade)) {
|
||||||
$mapping['cascade'] = $this->_getCascadeMappings($manyToManyElement->cascade);
|
$mapping['cascade'] = $this->_getCascadeMappings($manyToManyElement->cascade);
|
||||||
}
|
}
|
||||||
|
if (isset($manyToManyElement->{'orphan-removal'})) {
|
||||||
|
$mapping['orphanRemoval'] = (bool)$manyToManyElement->{'orphan-removal'};
|
||||||
|
}
|
||||||
$metadata->mapManyToMany($mapping);
|
$metadata->mapManyToMany($mapping);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if ($xmlRoot->getName() == 'mapped-superclass') {
|
// Evaluate <lifecycle-listener...>
|
||||||
throw MappingException::notImplemented('Mapped superclasses are not yet supported.');
|
if (isset($xmlRoot->{'lifecycle-listener'})) {
|
||||||
|
foreach ($xmlRoot->{'lifecycle-listener'} as $lifecycleListener) {
|
||||||
|
$method = $class->getMethod((string)$lifecycleListener['method']);
|
||||||
|
if ($method->isPublic()) {
|
||||||
|
$metadata->addLifecycleCallback($method->getName(), constant('\Doctrine\ORM\Events::' . (string)$lifecycleListener['type']));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,9 +42,19 @@ class YamlDriver extends AbstractFileDriver
|
|||||||
|
|
||||||
public function loadMetadataForClass($className, ClassMetadata $metadata)
|
public function loadMetadataForClass($className, ClassMetadata $metadata)
|
||||||
{
|
{
|
||||||
|
$class = $metadata->getReflectionClass();
|
||||||
|
|
||||||
$element = $this->getElement($className);
|
$element = $this->getElement($className);
|
||||||
|
|
||||||
if ($element['type'] == 'entity') {
|
if ($element['type'] == 'entity') {
|
||||||
|
$metadata->setCustomRepositoryClass(
|
||||||
|
isset($element['repositoryClass']) ? $xmlRoot['repositoryClass'] : null
|
||||||
|
);
|
||||||
|
} else if ($element['type'] == 'mappedSuperclass') {
|
||||||
|
$metadata->isMappedSuperclass = true;
|
||||||
|
} else {
|
||||||
|
throw DoctrineException::updateMe("$className is no entity or mapped superclass.");
|
||||||
|
}
|
||||||
|
|
||||||
// Evaluate root level properties
|
// Evaluate root level properties
|
||||||
if (isset($element['table'])) {
|
if (isset($element['table'])) {
|
||||||
@ -57,6 +67,26 @@ class YamlDriver extends AbstractFileDriver
|
|||||||
$metadata->setInheritanceType($element['inheritanceType']);
|
$metadata->setInheritanceType($element['inheritanceType']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Evaluate discriminatorColumn
|
||||||
|
if (isset($element['discriminatorColumn'])) {
|
||||||
|
$discrColumn = $element['discriminatorColumn'];
|
||||||
|
$metadata->setDiscriminatorColumn(array(
|
||||||
|
'name' => $discrColumn['name'],
|
||||||
|
'type' => $discrColumn['type'],
|
||||||
|
'length' => $discrColumn['length']
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Evaluate discriminatorMap
|
||||||
|
if (isset($element['discriminatorMap'])) {
|
||||||
|
$metadata->setDiscriminatorMap($element['discriminatorMap']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Evaluate changeTrackingPolicy
|
||||||
|
if (isset($element['changeTrackingPolicy'])) {
|
||||||
|
$metadata->setChangeTrackingPolicy($element['changeTrackingPolicy']);
|
||||||
|
}
|
||||||
|
|
||||||
// Evaluate indexes
|
// Evaluate indexes
|
||||||
if (isset($element['indexes'])) {
|
if (isset($element['indexes'])) {
|
||||||
foreach ($element['indexes'] as $index) {
|
foreach ($element['indexes'] as $index) {
|
||||||
@ -236,8 +266,14 @@ class YamlDriver extends AbstractFileDriver
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if ($element['type'] == 'mapped-superclass') {
|
// Evaluate lifeCycleListener
|
||||||
throw MappingException::notImplemented('Mapped superclasses are not yet supported.');
|
if (isset($element['lifecycleListeners'])) {
|
||||||
|
foreach ($element['lifecycleListeners'] as $method => $type) {
|
||||||
|
$method = $class->getMethod($method);
|
||||||
|
if ($method->isPublic()) {
|
||||||
|
$metadata->addLifecycleCallback($method->getName(), constant('\Doctrine\ORM\Events::'.$type));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,6 +97,9 @@ class MappingDriverTest extends \Doctrine\Tests\OrmTestCase
|
|||||||
$this->assertTrue($class->associationMappings['groups'] instanceof \Doctrine\ORM\Mapping\ManyToManyMapping);
|
$this->assertTrue($class->associationMappings['groups'] instanceof \Doctrine\ORM\Mapping\ManyToManyMapping);
|
||||||
$this->assertTrue(isset($class->associationMappings['groups']));
|
$this->assertTrue(isset($class->associationMappings['groups']));
|
||||||
$this->assertTrue($class->associationMappings['groups']->isOwningSide);
|
$this->assertTrue($class->associationMappings['groups']->isOwningSide);
|
||||||
|
$this->assertEquals(count($class->lifecycleCallbacks), 2);
|
||||||
|
$this->assertEquals($class->lifecycleCallbacks['prePersist'][0], 'doStuffOnPrePersist');
|
||||||
|
$this->assertEquals($class->lifecycleCallbacks['postPersist'][0], 'doStuffOnPostPersist');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,4 +111,13 @@ class User {
|
|||||||
private $groups;
|
private $groups;
|
||||||
|
|
||||||
// ... rest of code omitted, irrelevant for the mapping tests
|
// ... rest of code omitted, irrelevant for the mapping tests
|
||||||
|
|
||||||
|
public function doStuffOnPrePersist()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function doStuffOnPostPersist()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
@ -7,9 +7,8 @@
|
|||||||
|
|
||||||
<entity name="Doctrine\Tests\ORM\Mapping\User" table="cms_users">
|
<entity name="Doctrine\Tests\ORM\Mapping\User" table="cms_users">
|
||||||
|
|
||||||
<lifecycle-callbacks>
|
<lifecycle-listener method="doStuffOnPrePersist" type="prePersist" />
|
||||||
<lifecycle-callback type="prePersist" method="onPrePersist" />
|
<lifecycle-listener method="doStuffOnPostPersist" type="postPersist" />
|
||||||
</lifecycle-callbacks>
|
|
||||||
|
|
||||||
<id name="id" type="integer" column="id">
|
<id name="id" type="integer" column="id">
|
||||||
<generator strategy="AUTO"/>
|
<generator strategy="AUTO"/>
|
||||||
|
@ -32,3 +32,6 @@ Doctrine\Tests\ORM\Mapping\User:
|
|||||||
inverseJoinColumns:
|
inverseJoinColumns:
|
||||||
group_id:
|
group_id:
|
||||||
referencedColumnName: id
|
referencedColumnName: id
|
||||||
|
lifecycleListeners:
|
||||||
|
doStuffOnPrePersist: prePersist
|
||||||
|
doStuffOnPostPersist: postPersist
|
Loading…
x
Reference in New Issue
Block a user