Permalink
Browse files

Add support to configure custom persister classes.

  • Loading branch information...
1 parent 300f195 commit 8c3ad50bb5ad51f9064543768745f3d85f4b7d64 @beberlei beberlei committed Oct 12, 2010
@@ -73,7 +73,8 @@
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="table" type="xs:NMTOKEN" />
<xs:attribute name="schema" type="xs:NMTOKEN" />
- <xs:attribute name="repository-class" type="xs:NMTOKEN"/>
+ <xs:attribute name="repository-class" type="xs:string"/>
+ <xs:attribute name="persister-class" type="xs:string"/>
<xs:attribute name="inheritance-type" type="orm:inheritance-type"/>
<xs:attribute name="change-tracking-policy" type="orm:change-tracking-policy" />
</xs:complexType>
@@ -296,6 +296,9 @@ public function __sleep()
if ($this->customRepositoryClassName) {
$serialized[] = 'customRepositoryClassName';
}
+ if ($this->customPersisterClassName) {
+ $serialized[] = 'customPersisterClassName';
+ }
if ($this->inheritanceType != self::INHERITANCE_TYPE_NONE) {
$serialized[] = 'inheritanceType';
@@ -177,6 +177,13 @@ class ClassMetadataInfo
public $customRepositoryClassName;
/**
+ * The name of the custom persister class used for the entity class.
+ *
+ * @var string
+ */
+ public $customPersisterClassName;
+
+ /**
* READ-ONLY: Whether this class describes the mapping of a mapped superclass.
*
* @var boolean
@@ -1362,6 +1369,16 @@ public function setCustomRepositoryClass($repositoryClassName)
}
/**
+ * Registers a custom persister class for the entity class.
+ *
+ * @param string $persisterClassName
+ */
+ public function setCustomPersisterClass($persisterClassName)
+ {
+ $this->customPersisterClassName = $persisterClassName;
+ }
+
+ /**
* Dispatches the lifecycle event of the given entity to the registered
* lifecycle callbacks and lifecycle listeners.
*
@@ -132,6 +132,7 @@ public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
if (isset($classAnnotations['Doctrine\ORM\Mapping\Entity'])) {
$entityAnnot = $classAnnotations['Doctrine\ORM\Mapping\Entity'];
$metadata->setCustomRepositoryClass($entityAnnot->repositoryClass);
+ $metadata->setCustomPersisterClass($entityAnnot->persisterClass);
} else if (isset($classAnnotations['Doctrine\ORM\Mapping\MappedSuperclass'])) {
$metadata->isMappedSuperclass = true;
} else {
@@ -27,6 +27,7 @@
final class Entity extends Annotation {
public $repositoryClass;
+ public $persisterClass;
}
final class MappedSuperclass extends Annotation {}
final class InheritanceType extends Annotation {}
@@ -55,6 +55,9 @@ public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
$metadata->setCustomRepositoryClass(
isset($xmlRoot['repository-class']) ? (string)$xmlRoot['repository-class'] : null
);
+ $metadata->setCustomPersisterClass(
+ isset($xmlRoot['persister-class']) ? (string)$xmlRoot['persister-class'] : null
+ );
} else if ($xmlRoot->getName() == 'mapped-superclass') {
$metadata->isMappedSuperclass = true;
} else {
@@ -49,6 +49,9 @@ public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
$metadata->setCustomRepositoryClass(
isset($element['repositoryClass']) ? $element['repositoryClass'] : null
);
+ $metadata->setCustomPersisterClass(
+ isset($element['persisterClass']) ? $element['persisterClass'] : null
+ );
} else if ($element['type'] == 'mappedSuperclass') {
$metadata->isMappedSuperclass = true;
} else {
@@ -2108,7 +2108,10 @@ public function getEntityPersister($entityName)
{
if ( ! isset($this->persisters[$entityName])) {
$class = $this->em->getClassMetadata($entityName);
- if ($class->isInheritanceTypeNone()) {
+ if ($class->customPersisterClassName) {
+ $persisterClass = $class->customPersisterClassName;
+ $persister = new $persisterClass($this->em, $class);
+ } else if ($class->isInheritanceTypeNone()) {
$persister = new Persisters\BasicEntityPersister($this->em, $class);
} else if ($class->isInheritanceTypeSingleTable()) {
$persister = new Persisters\SingleTablePersister($this->em, $class);
@@ -5,7 +5,7 @@
use Doctrine\Common\Collections\ArrayCollection;
/**
- * @Entity
+ * @Entity(persisterClass="Doctrine\Tests\Models\CMS\CmsUserPersister")
* @Table(name="cms_users")
*/
class CmsUser
@@ -117,3 +117,9 @@ public function setAddress(CmsAddress $address) {
}
}
}
+
+/** With this we check if usage throws no errors (very evil indirect test?) */
+class CmsUserPersister extends \Doctrine\ORM\Persisters\BasicEntityPersister
+{
+
+}
@@ -42,6 +42,18 @@ public function testEntityTableNameAndInheritance($class)
}
/**
+ * @depends testLoadMapping
+ * @param ClassMetadata $class
+ */
+ public function testEntityRepositoryAndPersister($class)
+ {
+ $this->assertEquals('Doctrine\Tests\ORM\Mapping\UserRepository', $class->customRepositoryClassName);
+ $this->assertEquals('Doctrine\Tests\ORM\Mapping\UserPersister', $class->customPersisterClassName);
+
+ return $class;
+ }
+
+ /**
* @depends testEntityTableNameAndInheritance
* @param ClassMetadata $class
*/
@@ -294,7 +306,7 @@ public function testDiscriminatorColumnDefaults()
}
/**
- * @Entity
+ * @Entity(persisterClass="Doctrine\Tests\ORM\Mapping\UserPersister", repositoryClass="Doctrine\Tests\ORM\Mapping\UserRepository")
* @HasLifecycleCallbacks
* @Table(
* name="cms_users",
@@ -367,6 +379,8 @@ public function doStuffOnPostPersist()
public static function loadMetadata(ClassMetadataInfo $metadata)
{
+ $metadata->setCustomPersisterClass("Doctrine\Tests\ORM\Mapping\UserPersister");
+ $metadata->setCustomRepositoryClass("Doctrine\Tests\ORM\Mapping\UserRepository");
$metadata->setInheritanceType(ClassMetadataInfo::INHERITANCE_TYPE_NONE);
$metadata->setPrimaryTable(array(
'name' => 'cms_users',
@@ -482,6 +496,16 @@ public static function loadMetadata(ClassMetadataInfo $metadata)
}
}
+class UserPersister extends \Doctrine\ORM\Persisters\BasicEntityPersister
+{
+
+}
+
+class UserRepository extends \Doctrine\ORM\EntityRepository
+{
+
+}
+
/**
* @Entity
* @InheritanceType("SINGLE_TABLE")
@@ -2,6 +2,8 @@
use Doctrine\ORM\Mapping\ClassMetadataInfo;
+$metadata->setCustomPersisterClass("Doctrine\Tests\ORM\Mapping\UserPersister");
+$metadata->setCustomRepositoryClass("Doctrine\Tests\ORM\Mapping\UserRepository");
$metadata->setInheritanceType(ClassMetadataInfo::INHERITANCE_TYPE_NONE);
$metadata->setPrimaryTable(array(
'name' => 'cms_users',
@@ -5,7 +5,9 @@
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
- <entity name="Doctrine\Tests\ORM\Mapping\User" table="cms_users">
+ <entity name="Doctrine\Tests\ORM\Mapping\User" table="cms_users"
+ repository-class="Doctrine\Tests\ORM\Mapping\UserRepository"
+ persister-class="Doctrine\Tests\ORM\Mapping\UserPersister">
<indexes>
<index name="name_idx" columns="name"/>
@@ -1,6 +1,8 @@
Doctrine\Tests\ORM\Mapping\User:
type: entity
table: cms_users
+ persisterClass: Doctrine\Tests\ORM\Mapping\UserPersister
+ repositoryClass: Doctrine\Tests\ORM\Mapping\UserRepository
id:
id:
type: integer

5 comments on commit 8c3ad50

@Powerhamster
Contributor

Are there any plans of merging this into the master branch in the near future?

@KonstantinKuklin
Contributor

+1 for the question

@Ocramius
Member

@Powerhamster @KonstantinKuklin probably not ATM

@Koc
Contributor
Koc commented on 8c3ad50 May 22, 2015

@KonstantinKuklin What use case of this feature?

@KonstantinKuklin
Contributor

@Koc common persisters in fully related to SQL, but if I tried to use another protocol to get data (for example get data from mysql database via plugin HandlerSocket) I can`t do it without some piece of magic and hacks inside doctrine engine.
Current list of persisters are limit data sources which I can use with doctrine, because my own driver must be SQL similar and very hard to use non SQL.

Please sign in to comment.