Commit 957a6b2c authored by romanb's avatar romanb

added missing annotation driver class. small refactorings. added first...

added missing annotation driver class. small refactorings. added first ClassMetadata serialization tests.
parent bcf01102
......@@ -19,7 +19,7 @@
* <http://www.phpdoctrine.org>.
*/
#namespace Doctrine::ORM::Exceptions;
#namespace Doctrine\ORM\Exceptions;
/**
* A MappingException indicates that something is wrong with the mapping setup.
......@@ -39,11 +39,6 @@ class Doctrine_ORM_Exceptions_MappingException extends Doctrine_ORM_Exceptions_O
return new self("The inheritance type '$type' does not exist.");
}
public static function invalidInheritanceOption($name)
{
return new self("The inheritance option '$name' does not exist.");
}
public static function generatorNotAllowedWithCompositeId()
{
return new self("Id generators can't be used with a composite id.");
......
......@@ -28,7 +28,7 @@
* @since 2.0
* @todo Rename to AssociationMapping.
*/
abstract class Doctrine_ORM_Mapping_AssociationMapping implements Serializable
abstract class Doctrine_ORM_Mapping_AssociationMapping
{
const FETCH_MANUAL = 1;
const FETCH_LAZY = 2;
......@@ -445,18 +445,5 @@ abstract class Doctrine_ORM_Mapping_AssociationMapping implements Serializable
}
abstract public function lazyLoadFor($entity);
/* Serializable implementation */
public function serialize()
{
return "";
}
public function unserialize($serialized)
{
return true;
}
}
?>
\ No newline at end of file
This diff is collapsed.
......@@ -109,7 +109,9 @@ class Doctrine_ORM_Mapping_ClassMetadataFactory
$parent = $class;
foreach ($parentClasses as $subclassName) {
$subClass = new Doctrine_ORM_Mapping_ClassMetadata($subclassName);
$subClass->setInheritanceType($parent->getInheritanceType(), $parent->getInheritanceOptions());
$subClass->setInheritanceType($parent->getInheritanceType());
$subClass->setDiscriminatorMap($parent->getDiscriminatorMap());
$subClass->setDiscriminatorColumn($parent->getDiscriminatorColumn());
$this->_addInheritedFields($subClass, $parent);
$this->_addInheritedRelations($subClass, $parent);
$this->_loadClassMetadata($subClass, $subclassName);
......
<?php
#namespace Doctrine\ORM\Mapping\Driver;
/* Addendum annotation API */
require_once dirname(__FILE__) . '/addendum/annotations.php';
Addendum::setRawMode(false);
Addendum::setParsedAnnotations(array('DoctrineEntity', 'DoctrineInheritanceType',
'DoctrineDiscriminatorColumn', 'DoctrineDiscriminatorMap',
'DoctrineSubClasses', 'DoctrineTransient', 'DoctrineId',
'DoctrineIdGenerator', 'DoctrineColumn', 'DoctrineOneToOne',
'DoctrineOneToMany', 'DoctrineManyToOne', 'DoctrineManyToMany'));
/**
* The AnnotationDriver reads the mapping metadata from docblock annotations.
*
* @author robo
*/
class Doctrine_ORM_Mapping_Driver_AnnotationDriver {
/**
* Loads the metadata for the specified class into the provided container.
*/
public function loadMetadataForClass($className, Doctrine_ORM_Mapping_ClassMetadata $metadata)
{
$annotClass = new ReflectionAnnotatedClass($className);
if (($entityAnnot = $annotClass->getAnnotation('DoctrineEntity')) === false) {
throw new Doctrine_ORM_Exceptions_MappingException("$className is no entity.");
}
$metadata->setTableName($entityAnnot->tableName);
$metadata->setCustomRepositoryClass($entityAnnot->repositoryClass);
if ($inheritanceTypeAnnot = $annotClass->getAnnotation('DoctrineInheritanceType')) {
$metadata->setInheritanceType($inheritanceTypeAnnot->value);
}
if ($discrColumnAnnot = $annotClass->getAnnotation('DoctrineDiscriminatorColumn')) {
$metadata->setDiscriminatorColumn(array(
'name' => $discrColumnAnnot->name,
'type' => $discrColumnAnnot->type,
'length' => $discrColumnAnnot->length
));
}
if ($discrValueAnnot = $annotClass->getAnnotation('DoctrineDiscriminatorMap')) {
$metadata->setDiscriminatorMap((array)$discrValueAnnot->value);
}
if ($subClassesAnnot = $annotClass->getAnnotation('DoctrineSubClasses')) {
$metadata->setSubclasses($subClassesAnnot->value);
}
foreach ($annotClass->getProperties() as $property) {
if ($property->hasAnnotation('DoctrineTransient')) {
continue;
}
$mapping = array();
$mapping['fieldName'] = $property->getName();
if ($columnAnnot = $property->getAnnotation('DoctrineColumn')) {
if ($columnAnnot->type == null) {
throw new Doctrine_ORM_Exceptions_MappingException("Missing type on property " . $property->getName());
}
$mapping['type'] = $columnAnnot->type;
$mapping['length'] = $columnAnnot->length;
if ($idAnnot = $property->getAnnotation('DoctrineId')) {
$mapping['id'] = true;
}
if ($idGeneratorAnnot = $property->getAnnotation('DoctrineIdGenerator')) {
$mapping['idGenerator'] = $idGeneratorAnnot->value;
}
$metadata->mapField($mapping);
} else if ($oneToOneAnnot = $property->getAnnotation('DoctrineOneToOne')) {
$mapping['targetEntity'] = $oneToOneAnnot->targetEntity;
$mapping['joinColumns'] = $oneToOneAnnot->joinColumns;
$mapping['mappedBy'] = $oneToOneAnnot->mappedBy;
$mapping['cascade'] = $oneToOneAnnot->cascade;
$metadata->mapOneToOne($mapping);
} else if ($oneToManyAnnot = $property->getAnnotation('DoctrineOneToMany')) {
$mapping['mappedBy'] = $oneToManyAnnot->mappedBy;
$mapping['targetEntity'] = $oneToManyAnnot->targetEntity;
$metadata->mapOneToMany($mapping);
} else if ($manyToOneAnnot = $property->getAnnotation('DoctrineManyToOne')) {
$mapping['joinColumns'] = $manyToOneAnnot->joinColumns;
$mapping['targetEntity'] = $manyToOneAnnot->targetEntity;
$metadata->mapManyToOne($mapping);
} else if ($manyToManyAnnot = $property->getAnnotation('DoctrineManyToMany')) {
$mapping['targetEntity'] = $manyToManyAnnot->targetEntity;
$mapping['joinColumns'] = $manyToManyAnnot->joinColumns;
$mapping['inverseJoinColumns'] = $manyToManyAnnot->inverseJoinColumns;
$mapping['joinTable'] = $manyToManyAnnot->joinTable;
$mapping['mappedBy'] = $manyToManyAnnot->mappedBy;
$metadata->mapManyToMany($mapping);
} else {
throw new Doctrine_ORM_Exceptions_MappingException($className);
}
}
}
}
/* Annotations */
final class DoctrineEntity extends Annotation {
public $tableName;
public $repositoryClass;
public $inheritanceType;
}
final class DoctrineInheritanceType extends Annotation {}
final class DoctrineDiscriminatorColumn extends Annotation {
public $name;
public $type;
public $length;
}
final class DoctrineDiscriminatorMap extends Annotation {}
final class DoctrineSubClasses extends Annotation {}
final class DoctrineTransient extends Annotation {}
final class DoctrineId extends Annotation {}
final class DoctrineIdGenerator extends Annotation {}
final class DoctrineColumn extends Annotation {
public $type;
public $length;
public $unique;
public $notnull;
}
final class DoctrineOneToOne extends Annotation {
public $targetEntity;
public $mappedBy;
public $joinColumns;
public $cascade;
}
final class DoctrineOneToMany extends Annotation {
public $mappedBy;
public $targetEntity;
public $cascade;
}
final class DoctrineManyToOne extends Annotation {
public $targetEntity;
public $joinColumns;
public $cascade;
}
final class DoctrineManyToMany extends Annotation {
public $targetEntity;
public $joinColumns;
public $inverseJoinColumns;
public $joinTable;
public $mappedBy;
public $cascade;
}
......@@ -11,6 +11,6 @@ class Doctrine_ORM_Mapping_Driver_YamlDriver
*/
public function loadMetadataForClass($className, Doctrine_ORM_Mapping_ClassMetadata $metadata)
{
throw new Doctrine_ClassMetadata_Exception("YAML driver not yet implemented.");
throw new Doctrine_Exception("YAML driver not yet implemented.");
}
}
\ No newline at end of file
<?php
//@deprecated
class Doctrine_ClassMetadata_Exception extends Doctrine_Exception {}
\ No newline at end of file
......@@ -84,7 +84,7 @@ class Doctrine_ORM_Mapping_OneToOneMapping extends Doctrine_ORM_Mapping_Associat
if ($this->isOwningSide()) {
if ( ! isset($mapping['joinColumns'])) {
throw Doctrine_MappingException::invalidMapping($this->_sourceFieldName);
throw Doctrine_ORM_Exceptions_MappingException::invalidMapping($this->_sourceFieldName);
}
$this->_sourceToTargetKeyColumns = $mapping['joinColumns'];
$this->_targetToSourceKeyColumns = array_flip($this->_sourceToTargetKeyColumns);
......
......@@ -282,9 +282,9 @@ abstract class Doctrine_ORM_Persisters_AbstractEntityPersister
// Populate the discriminator column on insert in Single & Class Table Inheritance
if ($isInsert && ($this->_classMetadata->isInheritanceTypeJoined() ||
$this->_classMetadata->isInheritanceTypeSingleTable())) {
$discColumn = $this->_classMetadata->getInheritanceOption('discriminatorColumn');
$discMap = $this->_classMetadata->getInheritanceOption('discriminatorMap');
$result[$discColumn] = array_search($this->_entityName, $discMap);
$discColumn = $this->_classMetadata->getDiscriminatorColumn();
$discMap = $this->_classMetadata->getDiscriminatorMap();
$result[$discColumn['name']] = array_search($this->_entityName, $discMap);
}
}
......
......@@ -190,13 +190,13 @@ class Doctrine_ORM_Persisters_JoinedSubclassPersister extends Doctrine_ORM_Persi
{
$classMetadata = $this->_classMetadata;
$conn = $this->_conn;
$fields = array($classMetadata->getInheritanceOption('discriminatorColumn'));
$discrColumn = $classMetadata->getDiscriminatorColumn();
$fields = array($discrColumn['name']);
if ($classMetadata->getSubclasses()) {
foreach ($classMetadata->getSubclasses() as $subClass) {
$fields = array_merge($conn->getClassMetadata($subClass)->getFieldNames(), $fields);
}
}
return array_unique($fields);
}
......
......@@ -1077,15 +1077,15 @@ class Doctrine_ORM_UnitOfWork
{
$class = $this->_em->getClassMetadata($className);
$discCol = $class->getInheritanceOption('discriminatorColumn');
$discCol = $class->getDiscriminatorColumn();
if ( ! $discCol) {
return $className;
}
$discMap = $class->getInheritanceOption('discriminatorMap');
$discMap = $class->getDiscriminatorMap();
if (isset($data[$discCol], $discMap[$data[$discCol]])) {
return $discMap[$data[$discCol]];
if (isset($data[$discCol['name']], $discMap[$data[$discCol['name']]])) {
return $discMap[$data[$discCol['name']]];
} else {
return $className;
}
......
......@@ -17,6 +17,7 @@ require_once 'Orm/UnitOfWorkTest.php';
require_once 'Orm/EntityManagerTest.php';
require_once 'Orm/EntityPersisterTest.php';
require_once 'Orm/CommitOrderCalculatorTest.php';
require_once 'Orm/ClassMetadataTest.php';
class Orm_AllTests
{
......@@ -33,6 +34,7 @@ class Orm_AllTests
$suite->addTestSuite('Orm_EntityManagerTest');
$suite->addTestSuite('Orm_EntityPersisterTest');
$suite->addTestSuite('Orm_CommitOrderCalculatorTest');
$suite->addTestSuite('Orm_ClassMetadataTest');
$suite->addTest(Orm_Query_AllTests::suite());
$suite->addTest(Orm_Hydration_AllTests::suite());
......
<?php
require_once 'lib/DoctrineTestInit.php';
class Orm_ClassMetadataTest extends Doctrine_OrmTestCase
{
protected function setUp() {
;
}
protected function tearDown() {
;
}
public function testClassMetadataInstanceSerialization() {
$cm = new Doctrine_ORM_Mapping_ClassMetadata('CmsUser');
// Test initial state
$this->assertTrue(count($cm->getReflectionProperties()) > 0);
$this->assertTrue($cm->getReflectionClass() instanceof ReflectionClass);
$this->assertEquals('CmsUser', $cm->getClassName());
$this->assertEquals('CmsUser', $cm->getRootClassName());
$this->assertEquals(array(), $cm->getSubclasses());
$this->assertEquals(array(), $cm->getParentClasses());
// Customize state
$cm->setSubclasses(array("One", "Two", "Three"));
$cm->setParentClasses(array("UserParent"));
$cm->setCustomRepositoryClass("UserRepository");
$cm->setDiscriminatorColumn(array('name' => 'disc', 'type' => 'integer'));
$cm->mapOneToOne(array('fieldName' => 'foo', 'targetEntity' => 'Bar', 'mappedBy' => 'foo'));
$this->assertTrue($cm->getAssociationMapping('foo') instanceof Doctrine_ORM_Mapping_OneToOneMapping);
$this->assertEquals(1, count($cm->getAssociationMappings()));
$serialized = serialize($cm);
$cm = unserialize($serialized);
// Check state
$this->assertTrue(count($cm->getReflectionProperties()) > 0);
$this->assertTrue($cm->getReflectionClass() instanceof ReflectionClass);
$this->assertEquals('CmsUser', $cm->getClassName());
$this->assertEquals('UserParent', $cm->getRootClassName());
$this->assertEquals(array('One', 'Two', 'Three'), $cm->getSubclasses());
$this->assertEquals(array('UserParent'), $cm->getParentClasses());
$this->assertEquals('UserRepository', $cm->getCustomRepositoryClass());
$this->assertEquals(array('name' => 'disc', 'type' => 'integer'), $cm->getDiscriminatorColumn());
$this->assertTrue($cm->getAssociationMapping('foo') instanceof Doctrine_ORM_Mapping_OneToOneMapping);
$this->assertEquals(1, count($cm->getAssociationMappings()));
$oneOneMapping = $cm->getAssociationMapping('foo');
$this->assertEquals('foo', $oneOneMapping->getSourceFieldName());
$this->assertEquals('Bar', $oneOneMapping->getTargetEntityName());
}
public function testTransientEntityIsManaged()
{
;
}
}
\ No newline at end of file
<?php
require_once 'lib/DoctrineTestInit.php';
class Orm_ClassMetadataTestCase extends Doctrine_OrmTestCase
{
protected function setUp() {
;
}
protected function tearDown() {
;
}
public function testTransientEntityIsManaged()
{
;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment