Skip to content

Instantly share code, notes, and snippets.

@doctrinebot
Created December 13, 2015 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doctrinebot/20af4f7580b119891222 to your computer and use it in GitHub Desktop.
Save doctrinebot/20af4f7580b119891222 to your computer and use it in GitHub Desktop.
Attachments to Doctrine Jira Issue DDC-447 - https://github.com/doctrine/doctrine2/issues/4948
Index: tests/Doctrine/Tests/Models/Company/CompanyAuction.php
===================================================================
--- tests/Doctrine/Tests/Models/Company/CompanyAuction.php (revision 7431)
+++ tests/Doctrine/Tests/Models/Company/CompanyAuction.php (working copy)
@@ -2,7 +2,7 @@
namespace Doctrine\Tests\Models\Company;
-/** @Entity @Table(name="company_auctions") */
+/** @Entity(discriminatorValue="auction") @Table(name="company_auctions") */
class CompanyAuction extends CompanyEvent {
/** @Column(type="string") */
private $data;
Index: tests/Doctrine/Tests/Models/Company/CompanyRaffle.php
===================================================================
--- tests/Doctrine/Tests/Models/Company/CompanyRaffle.php (revision 7431)
+++ tests/Doctrine/Tests/Models/Company/CompanyRaffle.php (working copy)
@@ -2,7 +2,7 @@
namespace Doctrine\Tests\Models\Company;
-/** @Entity @Table(name="company_raffles") */
+/** @Entity(discriminatorValue="raffle") @Table(name="company_raffles") */
class CompanyRaffle extends CompanyEvent {
/** @Column(type="string") */
private $data;
Index: tests/Doctrine/Tests/Models/Company/CompanyEvent.php
===================================================================
--- tests/Doctrine/Tests/Models/Company/CompanyEvent.php (revision 7431)
+++ tests/Doctrine/Tests/Models/Company/CompanyEvent.php (working copy)
@@ -6,7 +6,6 @@
* @Entity @Table(name="company_events")
* @InheritanceType("JOINED")
* @DiscriminatorColumn(name="event_type", type="string")
- * @DiscriminatorMap({"auction" = "CompanyAuction", "raffle" = "CompanyRaffle"})
*/
class CompanyEvent {
/**
Index: lib/Doctrine/ORM/Mapping/MappingException.php
===================================================================
--- lib/Doctrine/ORM/Mapping/MappingException.php (revision 7431)
+++ lib/Doctrine/ORM/Mapping/MappingException.php (working copy)
@@ -185,4 +185,12 @@
"does not exist."
);
}
+
+ public static function entityTypeRequiresParentClass($className)
+ {
+ return new self(
+ "Entity type annotation requires that '$className' extends a parent class in order ".
+ "to specify a inheritance discriminator column value."
+ );
+ }
}
\ No newline at end of file
Index: lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php
===================================================================
--- lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php (revision 7431)
+++ lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php (working copy)
@@ -281,7 +281,12 @@
$eventArgs = new \Doctrine\ORM\Event\LoadClassMetadataEventArgs($class);
$this->_evm->dispatchEvent(Events::loadClassMetadata, $eventArgs);
}
-
+
+ if ($class->explicitDiscriminatorValue && $parentClasses[0] !== $className) {
+ $class->discriminatorValue = $class->explicitDiscriminatorValue;
+ $this->getMetadataFor($parentClasses[0])->addDiscriminatorMapValue($class->explicitDiscriminatorValue, $className);
+ }
+
$this->_loadedMetadata[$className] = $class;
$parent = $class;
Index: lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
===================================================================
--- lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php (revision 7431)
+++ lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php (working copy)
@@ -266,6 +266,17 @@
public $discriminatorValue;
/**
+ * READ-ONLY: The explicitly set disciminator value on the child entity class.
+ *
+ * <b>This does only apply to the JOINED and SINGLE_TABLE inheritance mapping strategies
+ * where a discriminator column is used.</b>
+ *
+ * @var mixed
+ * @see discriminatorColumn
+ */
+ public $explicitDiscriminatorValue;
+
+ /**
* READ-ONLY: The discriminator map of all mapped classes in the hierarchy.
*
* <b>This does only apply to the JOINED and SINGLE_TABLE inheritance mapping strategies
@@ -1251,19 +1262,24 @@
public function setDiscriminatorMap(array $map)
{
foreach ($map as $value => $className) {
- if (strpos($className, '\\') === false && strlen($this->namespace)) {
- $className = $this->namespace . '\\' . $className;
+ $this->addDiscriminatorMapValue($value, $className);
+ }
+ }
+
+ public function addDiscriminatorMapValue($value, $className)
+ {
+ if (strpos($className, '\\') === false && strlen($this->namespace)) {
+ $className = $this->namespace . '\\' . $className;
+ }
+ $this->discriminatorMap[$value] = $className;
+ if ($this->name == $className) {
+ $this->discriminatorValue = $value;
+ } else {
+ if ( ! class_exists($className)) {
+ throw MappingException::invalidClassInDiscriminatorMap($className, $this->name);
}
- $this->discriminatorMap[$value] = $className;
- if ($this->name == $className) {
- $this->discriminatorValue = $value;
- } else {
- if ( ! class_exists($className)) {
- throw MappingException::invalidClassInDiscriminatorMap($className, $this->name);
- }
- if (is_subclass_of($className, $this->name)) {
- $this->subClasses[] = $className;
- }
+ if (is_subclass_of($className, $this->name)) {
+ $this->subClasses[] = $className;
}
}
}
Index: lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php
===================================================================
--- lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php (revision 7431)
+++ lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php (working copy)
@@ -27,6 +27,7 @@
final class Entity extends Annotation {
public $repositoryClass;
+ public $discriminatorValue;
}
final class MappedSuperclass extends Annotation {}
final class InheritanceType extends Annotation {}
Index: lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php
===================================================================
--- lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php (revision 7431)
+++ lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php (working copy)
@@ -137,6 +137,14 @@
if (isset($classAnnotations['Doctrine\ORM\Mapping\Entity'])) {
$entityAnnot = $classAnnotations['Doctrine\ORM\Mapping\Entity'];
$metadata->setCustomRepositoryClass($entityAnnot->repositoryClass);
+
+ if (isset($entityAnnot->discriminatorValue) && $entityAnnot->discriminatorValue) {
+ if ($parent = $metadata->reflClass->getParentClass()) {
+ $metadata->explicitDiscriminatorValue = $entityAnnot->discriminatorValue;
+ } else {
+ throw MappingException::entityTypeRequiresParentClass($className);
+ }
+ }
} else if (isset($classAnnotations['Doctrine\ORM\Mapping\MappedSuperclass'])) {
$metadata->isMappedSuperclass = true;
} else {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment