Skip to content

Commit

Permalink
Add support for custom ID generator in XML
Browse files Browse the repository at this point in the history
  • Loading branch information
velovint committed Nov 28, 2011
1 parent 6bcfaed commit 2b97f79
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
15 changes: 15 additions & 0 deletions doctrine-mapping.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
<xs:enumeration value="SEQUENCE"/>
<xs:enumeration value="IDENTITY"/>
<xs:enumeration value="AUTO"/>
<xs:enumeration value="CUSTOM" />
</xs:restriction>
</xs:simpleType>

Expand Down Expand Up @@ -253,6 +254,7 @@
<xs:sequence>
<xs:element name="generator" type="orm:generator" minOccurs="0" />
<xs:element name="sequence-generator" type="orm:sequence-generator" minOccurs="0" maxOccurs="1" />
<xs:element name="custom-generator" type="orm:custom-generator" minOccurs="0" maxOccurs="1" />
<xs:any minOccurs="0" maxOccurs="unbounded" namespace="##other"/>
</xs:sequence>
<xs:attribute name="name" type="xs:NMTOKEN" use="required" />
Expand All @@ -271,6 +273,19 @@
<xs:attribute name="initial-value" type="xs:integer" use="optional" default="1" />
<xs:anyAttribute namespace="##other"/>
</xs:complexType>

<xs:complexType name="custom-generator">
<xs:sequence>
<xs:element name="args" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="arg" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="class" type="xs:NMTOKEN" use="required" />
</xs:complexType>

<xs:complexType name="inverse-join-columns">
<xs:sequence>
Expand Down
10 changes: 10 additions & 0 deletions lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,16 @@ public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
'allocationSize' => (string)$seqGenerator['allocation-size'],
'initialValue' => (string)$seqGenerator['initial-value']
));
} else if (isset($idElement->{'custom-generator'})) {
$customGenerator = $idElement->{'custom-generator'};
$args = array();
foreach ($customGenerator->args->children() as $argument) {
$args[] = (string) $argument;
}
$metadata->setCustomGeneratorDefinition(array(
'class' => (string) $customGenerator['class'],
'args' => $args
));
} else if (isset($idElement->{'table-generator'})) {
throw MappingException::tableIdGeneratorNotImplemented($className);
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ public function testEntitySequence($class)
$class->sequenceGeneratorDefinition
);
}

public function testEntityCustomGenerator()
{
$class = $this->createClassMetadata('Doctrine\Tests\ORM\Mapping\Animal');

$this->assertEquals(ClassMetadata::GENERATOR_TYPE_CUSTOM,
$class->generatorType, "Generator Type");
$this->assertEquals(
array("class" => "stdClass", "args" => array("par1", "par2")),
$class->customGeneratorDefinition,
"Custom Generator Definition");
}


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,14 @@
<discriminator-mapping value="cat" class="Cat" />
<discriminator-mapping value="dog" class="Dog" />
</discriminator-map>
<id name="id" type="integer" column="id">
<generator strategy="CUSTOM"/>
<custom-generator class="stdClass">
<args>
<arg>par1</arg>
<arg>par2</arg>
</args>
</custom-generator>
</id>
</entity>
</doctrine-mapping>

0 comments on commit 2b97f79

Please sign in to comment.