From 73985fe62a21c4f6ce50c6b6f2e7260e4fbd5b27 Mon Sep 17 00:00:00 2001 From: romanb Date: Fri, 22 Aug 2008 09:37:03 +0000 Subject: [PATCH] added mapping check to onetomany. corrected test models. --- lib/Doctrine/Association/OneToMany.php | 18 ++++++++++++++++++ lib/Doctrine/MappingException.php | 5 +++++ tests/models/cms/CmsArticle.php | 1 + tests/models/cms/CmsComment.php | 6 ++++++ tests/models/forum/ForumBoard.php | 5 +---- tests/models/forum/ForumCategory.php | 6 ++---- 6 files changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/Doctrine/Association/OneToMany.php b/lib/Doctrine/Association/OneToMany.php index f5f1b5248..0ee2c23a4 100644 --- a/lib/Doctrine/Association/OneToMany.php +++ b/lib/Doctrine/Association/OneToMany.php @@ -67,6 +67,24 @@ class Doctrine_Association_OneToMany extends Doctrine_Association $this->_isOwningSide = false; } + /** + * Validates and completed the mapping. + * + * @param array $mapping The mapping to validate and complete. + * @return array The validated and completed mapping. + * @override + */ + protected function _validateAndCompleteMapping(array $mapping) + { + $mapping = parent::_validateAndCompleteMapping($mapping); + // one-side MUST be inverse (must have mappedBy) + if ( ! isset($mapping['mappedBy'])) { + throw Doctrine_MappingException::oneToManyRequiresMappedBy($mapping['fieldName']); + } + + return $mapping; + } + /** * Whether orphaned elements (removed from the collection) should be deleted. * diff --git a/lib/Doctrine/MappingException.php b/lib/Doctrine/MappingException.php index 902c65a00..47e8636d3 100644 --- a/lib/Doctrine/MappingException.php +++ b/lib/Doctrine/MappingException.php @@ -47,6 +47,11 @@ class Doctrine_MappingException extends Doctrine_Exception { return new self("No mapping found for field '$fieldName'."); } + + public static function oneToManyRequiresMappedBy($fieldName) + { + return new self("OneToMany mapping on field '$fieldName' requires the 'mappedBy' attribute."); + } } ?> \ No newline at end of file diff --git a/tests/models/cms/CmsArticle.php b/tests/models/cms/CmsArticle.php index 98714e049..260fcbaf3 100755 --- a/tests/models/cms/CmsArticle.php +++ b/tests/models/cms/CmsArticle.php @@ -39,6 +39,7 @@ class CmsArticle extends Doctrine_Entity $mapping->mapOneToMany(array( 'fieldName' => 'comments', 'targetEntity' => 'CmsComment', + 'mappedBy' => 'article' )); $mapping->mapManyToOne(array( diff --git a/tests/models/cms/CmsComment.php b/tests/models/cms/CmsComment.php index 52663b0b9..55039dc13 100755 --- a/tests/models/cms/CmsComment.php +++ b/tests/models/cms/CmsComment.php @@ -34,5 +34,11 @@ class CmsComment extends Doctrine_Entity 'type' => 'integer', 'length' => 4 )); + + $mapping->mapManyToOne(array( + 'fieldName' => 'article', + 'targetEntity' => 'CmsArticle', + 'joinColumns' => array('article_id' => 'id') + )); } } diff --git a/tests/models/forum/ForumBoard.php b/tests/models/forum/ForumBoard.php index ca2c66c1d..bb443c78d 100755 --- a/tests/models/forum/ForumBoard.php +++ b/tests/models/forum/ForumBoard.php @@ -24,11 +24,8 @@ class ForumBoard extends Doctrine_Entity 'fieldName' => 'category_id', 'type' => 'integer' )); - - /*$mapping->hasOne('ForumCategory as category', - array('local' => 'category_id', 'foreign' => 'id'));*/ - $mapping->mapOneToOne(array( + $mapping->mapManyToOne(array( 'fieldName' => 'category', 'targetEntity' => 'ForumCategory', 'joinColumns' => array('category_id' => 'id') diff --git a/tests/models/forum/ForumCategory.php b/tests/models/forum/ForumCategory.php index 7ed242bb6..cf412b5b0 100755 --- a/tests/models/forum/ForumCategory.php +++ b/tests/models/forum/ForumCategory.php @@ -20,12 +20,10 @@ class ForumCategory extends Doctrine_Entity 'length' => 255 )); - /*$mapping->hasMany('ForumBoard as boards', array( - 'local' => 'id' , 'foreign' => 'category_id'));*/ - $mapping->mapOneToMany(array( 'fieldName' => 'boards', - 'targetEntity' => 'ForumBoard' + 'targetEntity' => 'ForumBoard', + 'mappedBy' => 'category' )); } }