1
0
mirror of synced 2025-03-17 13:33:52 +03:00

Improve Error Messages in ClassMetadata and UnitOfWork

This commit is contained in:
Benjamin Eberlei 2011-10-15 17:38:55 +02:00
parent 84dbb08502
commit cb28bfd484
8 changed files with 51 additions and 13 deletions

View File

@ -122,6 +122,25 @@ class ClassMetadata extends ClassMetadataInfo
$this->reflFields[$mapping['fieldName']] = $refProp; $this->reflFields[$mapping['fieldName']] = $refProp;
} }
/**
* Validates & completes the basic mapping information that is common to all
* association mappings (one-to-one, many-ot-one, one-to-many, many-to-many).
*
* @param array $mapping The mapping.
* @return array The updated mapping.
* @throws MappingException If something is wrong with the mapping.
*/
protected function _validateAndCompleteAssociationMapping(array $mapping)
{
$mapping = parent::_validateAndCompleteAssociationMapping($mapping);
if ( ! class_exists($mapping['targetEntity']) ) {
#throw MappingException::invalidTargetEntityClass($mapping['targetEntity'], $this->name, $mapping['fieldName']);
}
return $mapping;
}
/** /**
* Extracts the identifier values of an entity of this class. * Extracts the identifier values of an entity of this class.
* *

View File

@ -298,4 +298,9 @@ class MappingException extends \Doctrine\ORM\ORMException
{ {
return new self("Entity '" . $className . "' has no method '" . $methodName . "' to be registered as lifecycle callback."); return new self("Entity '" . $className . "' has no method '" . $methodName . "' to be registered as lifecycle callback.");
} }
public static function invalidTargetEntityClass($targetEntity, $sourceEntity, $associationName)
{
return new self("The target-entity " . $targetEntity . " cannot be found in '" . $sourceEntity."#".$associationName."'.");
}
} }

View File

@ -594,8 +594,8 @@ class UnitOfWork implements PropertyChangedListener
throw new InvalidArgumentException("A new entity was found through the relationship '" throw new InvalidArgumentException("A new entity was found through the relationship '"
. $assoc['sourceEntity'] . "#" . $assoc['fieldName'] . "' that was not" . $assoc['sourceEntity'] . "#" . $assoc['fieldName'] . "' that was not"
. " configured to cascade persist operations for entity: " . self::objToStr($entry) . "." . " configured to cascade persist operations for entity: " . self::objToStr($entry) . "."
. " Explicitly persist the new entity or configure cascading persist operations" . " Explicitly call EntityManager#persist() on this entity or configure to cascade persist "
. " on the relationship. If you cannot find out which entity causes the problem" . " the association. If you cannot find out which entity causes the problem"
. " implement '" . $assoc['targetEntity'] . "#__toString()' to get a clue."); . " implement '" . $assoc['targetEntity'] . "#__toString()' to get a clue.");
} }
$this->persistNew($targetClass, $entry); $this->persistNew($targetClass, $entry);

View File

@ -551,4 +551,8 @@ class Dog extends Animal
{ {
} }
} }
class Address {}
class Phonenumber {}
class Group {}

View File

@ -197,6 +197,7 @@ class MappedSuperclassBase {
private $mappedRelated1; private $mappedRelated1;
private $transient; private $transient;
} }
class MappedSuperclassRelated1 {}
/** @Entity */ /** @Entity */
class EntitySubClass2 extends MappedSuperclassBase { class EntitySubClass2 extends MappedSuperclassBase {

View File

@ -32,12 +32,12 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase
// Add a mapped field // Add a mapped field
$cm1->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true)); $cm1->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
// and a mapped association // and a mapped association
$cm1->mapOneToOne(array('fieldName' => 'other', 'targetEntity' => 'Other', 'mappedBy' => 'this')); $cm1->mapOneToOne(array('fieldName' => 'other', 'targetEntity' => 'TestEntity1', 'mappedBy' => 'this'));
// and an association on the owning side // and an association on the owning side
$joinColumns = array( $joinColumns = array(
array('name' => 'other_id', 'referencedColumnName' => 'id') array('name' => 'other_id', 'referencedColumnName' => 'id')
); );
$cm1->mapOneToOne(array('fieldName' => 'association', 'targetEntity' => 'Other', 'joinColumns' => $joinColumns)); $cm1->mapOneToOne(array('fieldName' => 'association', 'targetEntity' => 'TestEntity1', 'joinColumns' => $joinColumns));
// and an id generator type // and an id generator type
$cm1->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO); $cm1->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO);

View File

@ -29,7 +29,7 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase
$cm->setParentClasses(array("UserParent")); $cm->setParentClasses(array("UserParent"));
$cm->setCustomRepositoryClass("UserRepository"); $cm->setCustomRepositoryClass("UserRepository");
$cm->setDiscriminatorColumn(array('name' => 'disc', 'type' => 'integer')); $cm->setDiscriminatorColumn(array('name' => 'disc', 'type' => 'integer'));
$cm->mapOneToOne(array('fieldName' => 'phonenumbers', 'targetEntity' => 'Bar', 'mappedBy' => 'foo')); $cm->mapOneToOne(array('fieldName' => 'phonenumbers', 'targetEntity' => 'CmsAddress', 'mappedBy' => 'foo'));
$cm->markReadOnly(); $cm->markReadOnly();
$cm->addNamedQuery(array('name' => 'dql', 'query' => 'foo')); $cm->addNamedQuery(array('name' => 'dql', 'query' => 'foo'));
$this->assertEquals(1, count($cm->associationMappings)); $this->assertEquals(1, count($cm->associationMappings));
@ -52,7 +52,7 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase
$oneOneMapping = $cm->getAssociationMapping('phonenumbers'); $oneOneMapping = $cm->getAssociationMapping('phonenumbers');
$this->assertTrue($oneOneMapping['fetch'] == ClassMetadata::FETCH_LAZY); $this->assertTrue($oneOneMapping['fetch'] == ClassMetadata::FETCH_LAZY);
$this->assertEquals('phonenumbers', $oneOneMapping['fieldName']); $this->assertEquals('phonenumbers', $oneOneMapping['fieldName']);
$this->assertEquals('Doctrine\Tests\Models\CMS\Bar', $oneOneMapping['targetEntity']); $this->assertEquals('Doctrine\Tests\Models\CMS\CmsAddress', $oneOneMapping['targetEntity']);
$this->assertTrue($cm->isReadOnly); $this->assertTrue($cm->isReadOnly);
$this->assertEquals(array('dql' => 'foo'), $cm->namedQueries); $this->assertEquals(array('dql' => 'foo'), $cm->namedQueries);
} }

View File

@ -98,6 +98,8 @@ abstract class AbstractClassMetadataExporterTest extends \Doctrine\Tests\OrmTest
public function testExportDirectoryAndFilesAreCreated() public function testExportDirectoryAndFilesAreCreated()
{ {
$this->_deleteDirectory(__DIR__ . '/export/'.$this->_getType());
$type = $this->_getType(); $type = $this->_getType();
$metadataDriver = $this->_createMetadataDriver($type, __DIR__ . '/' . $type); $metadataDriver = $this->_createMetadataDriver($type, __DIR__ . '/' . $type);
$em = $this->_createEntityManager($metadataDriver); $em = $this->_createEntityManager($metadataDriver);
@ -320,12 +322,6 @@ abstract class AbstractClassMetadataExporterTest extends \Doctrine\Tests\OrmTest
$this->assertEquals('user', $class->associationMappings['address']['inversedBy']); $this->assertEquals('user', $class->associationMappings['address']['inversedBy']);
} }
public function __destruct()
{
$type = $this->_getType();
$this->_deleteDirectory(__DIR__ . '/export/'.$this->_getType());
}
protected function _deleteDirectory($path) protected function _deleteDirectory($path)
{ {
if (is_file($path)) { if (is_file($path)) {
@ -338,4 +334,17 @@ abstract class AbstractClassMetadataExporterTest extends \Doctrine\Tests\OrmTest
return rmdir($path); return rmdir($path);
} }
} }
}
class Address
{
}
class Phonenumber
{
}
class Group
{
} }