1
0
mirror of synced 2025-01-07 17:47:10 +03:00

added mapping check to onetomany. corrected test models.

This commit is contained in:
romanb 2008-08-22 09:37:03 +00:00
parent 0b80ec0bfd
commit 73985fe62a
6 changed files with 33 additions and 8 deletions

View File

@ -67,6 +67,24 @@ class Doctrine_Association_OneToMany extends Doctrine_Association
$this->_isOwningSide = false; $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. * Whether orphaned elements (removed from the collection) should be deleted.
* *

View File

@ -47,6 +47,11 @@ class Doctrine_MappingException extends Doctrine_Exception
{ {
return new self("No mapping found for field '$fieldName'."); 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.");
}
} }
?> ?>

View File

@ -39,6 +39,7 @@ class CmsArticle extends Doctrine_Entity
$mapping->mapOneToMany(array( $mapping->mapOneToMany(array(
'fieldName' => 'comments', 'fieldName' => 'comments',
'targetEntity' => 'CmsComment', 'targetEntity' => 'CmsComment',
'mappedBy' => 'article'
)); ));
$mapping->mapManyToOne(array( $mapping->mapManyToOne(array(

View File

@ -34,5 +34,11 @@ class CmsComment extends Doctrine_Entity
'type' => 'integer', 'type' => 'integer',
'length' => 4 'length' => 4
)); ));
$mapping->mapManyToOne(array(
'fieldName' => 'article',
'targetEntity' => 'CmsArticle',
'joinColumns' => array('article_id' => 'id')
));
} }
} }

View File

@ -25,10 +25,7 @@ class ForumBoard extends Doctrine_Entity
'type' => 'integer' 'type' => 'integer'
)); ));
/*$mapping->hasOne('ForumCategory as category', $mapping->mapManyToOne(array(
array('local' => 'category_id', 'foreign' => 'id'));*/
$mapping->mapOneToOne(array(
'fieldName' => 'category', 'fieldName' => 'category',
'targetEntity' => 'ForumCategory', 'targetEntity' => 'ForumCategory',
'joinColumns' => array('category_id' => 'id') 'joinColumns' => array('category_id' => 'id')

View File

@ -20,12 +20,10 @@ class ForumCategory extends Doctrine_Entity
'length' => 255 'length' => 255
)); ));
/*$mapping->hasMany('ForumBoard as boards', array(
'local' => 'id' , 'foreign' => 'category_id'));*/
$mapping->mapOneToMany(array( $mapping->mapOneToMany(array(
'fieldName' => 'boards', 'fieldName' => 'boards',
'targetEntity' => 'ForumBoard' 'targetEntity' => 'ForumBoard',
'mappedBy' => 'category'
)); ));
} }
} }