1
0
mirror of synced 2024-12-05 03:06:05 +03:00

Merge branch 'feature/#1229-to-one-association-builder'

Close #1229
This commit is contained in:
Marco Pivetta 2015-02-16 02:06:32 +00:00
commit a13143b1ac
3 changed files with 190 additions and 1 deletions

View File

@ -183,6 +183,18 @@ class AssociationBuilder
return $this;
}
/**
* Sets field as primary key.
*
* @return self
*/
public function isPrimaryKey()
{
$this->mapping['id'] = true;
return $this;
}
/**
* @return ClassMetadataBuilder
*

View File

@ -1461,6 +1461,10 @@ class ClassMetadataInfo implements ClassMetadata
}
if ( ! in_array($mapping['fieldName'], $this->identifier)) {
if (! isset($mapping['joinColumns'])) {
throw MappingException::illegalInverseIdentifierAssociation($this->name, $mapping['fieldName']);
}
if (count($mapping['joinColumns']) >= 2) {
throw MappingException::cannotMapCompositePrimaryKeyEntitiesAsForeignId(
$mapping['targetEntity'], $this->name, $mapping['fieldName']

View File

@ -332,6 +332,73 @@ class ClassMetadataBuilderTest extends \Doctrine\Tests\OrmTestCase
), $this->cm->associationMappings);
}
public function testCreateManyToOneWithIdentity()
{
$this->assertIsFluent(
$this
->builder
->createManyToOne('groups', 'Doctrine\Tests\Models\CMS\CmsGroup')
->addJoinColumn('group_id', 'id', true, false, 'CASCADE')
->cascadeAll()
->fetchExtraLazy()
->isPrimaryKey()
->build()
);
$this->assertEquals(
array(
'groups' => array(
'fieldName' => 'groups',
'targetEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsGroup',
'cascade' => array(
0 => 'remove',
1 => 'persist',
2 => 'refresh',
3 => 'merge',
4 => 'detach',
),
'fetch' => 4,
'joinColumns' => array(
0 =>
array(
'name' => 'group_id',
'referencedColumnName' => 'id',
'nullable' => true,
'unique' => false,
'onDelete' => 'CASCADE',
'columnDefinition' => NULL,
),
),
'type' => 2,
'mappedBy' => NULL,
'inversedBy' => NULL,
'isOwningSide' => true,
'sourceEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsUser',
'isCascadeRemove' => true,
'isCascadePersist' => true,
'isCascadeRefresh' => true,
'isCascadeMerge' => true,
'isCascadeDetach' => true,
'sourceToTargetKeyColumns' =>
array(
'group_id' => 'id',
),
'joinColumnFieldNames' =>
array(
'group_id' => 'group_id',
),
'targetToSourceKeyColumns' =>
array(
'id' => 'group_id',
),
'orphanRemoval' => false,
'id' => true
),
),
$this->cm->associationMappings
);
}
public function testCreateOneToOne()
{
$this->assertIsFluent(
@ -386,11 +453,91 @@ class ClassMetadataBuilderTest extends \Doctrine\Tests\OrmTestCase
array (
'id' => 'group_id',
),
'orphanRemoval' => false,
'orphanRemoval' => false
),
), $this->cm->associationMappings);
}
public function testCreateOneToOneWithIdentity()
{
$this->assertIsFluent(
$this
->builder
->createOneToOne('groups', 'Doctrine\Tests\Models\CMS\CmsGroup')
->addJoinColumn('group_id', 'id', true, false, 'CASCADE')
->cascadeAll()
->fetchExtraLazy()
->isPrimaryKey()
->build()
);
$this->assertEquals(
array(
'groups' => array(
'fieldName' => 'groups',
'targetEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsGroup',
'cascade' => array(
0 => 'remove',
1 => 'persist',
2 => 'refresh',
3 => 'merge',
4 => 'detach',
),
'fetch' => 4,
'id' => true,
'joinColumns' => array(
0 =>
array(
'name' => 'group_id',
'referencedColumnName' => 'id',
'nullable' => true,
'unique' => false,
'onDelete' => 'CASCADE',
'columnDefinition' => NULL,
),
),
'type' => 1,
'mappedBy' => NULL,
'inversedBy' => NULL,
'isOwningSide' => true,
'sourceEntity' => 'Doctrine\\Tests\\Models\\CMS\\CmsUser',
'isCascadeRemove' => true,
'isCascadePersist' => true,
'isCascadeRefresh' => true,
'isCascadeMerge' => true,
'isCascadeDetach' => true,
'sourceToTargetKeyColumns' =>
array(
'group_id' => 'id',
),
'joinColumnFieldNames' =>
array(
'group_id' => 'group_id',
),
'targetToSourceKeyColumns' =>
array(
'id' => 'group_id',
),
'orphanRemoval' => false
),
),
$this->cm->associationMappings
);
}
public function testThrowsExceptionOnCreateOneToOneWithIdentityOnInverseSide()
{
$this->setExpectedException('Doctrine\ORM\Mapping\MappingException');
$this
->builder
->createOneToOne('groups', 'Doctrine\Tests\Models\CMS\CmsGroup')
->mappedBy('test')
->fetchExtraLazy()
->isPrimaryKey()
->build();
}
public function testCreateManyToMany()
{
$this->assertIsFluent(
@ -474,6 +621,20 @@ class ClassMetadataBuilderTest extends \Doctrine\Tests\OrmTestCase
), $this->cm->associationMappings);
}
public function testThrowsExceptionOnCreateManyToManyWithIdentity()
{
$this->setExpectedException('Doctrine\ORM\Mapping\MappingException');
$this->builder->createManyToMany('groups', 'Doctrine\Tests\Models\CMS\CmsGroup')
->isPrimaryKey()
->setJoinTable('groups_users')
->addJoinColumn('group_id', 'id', true, false, 'CASCADE')
->addInverseJoinColumn('user_id', 'id')
->cascadeAll()
->fetchExtraLazy()
->build();
}
public function testCreateOneToMany()
{
$this->assertIsFluent(
@ -513,6 +674,18 @@ class ClassMetadataBuilderTest extends \Doctrine\Tests\OrmTestCase
), $this->cm->associationMappings);
}
public function testThrowsExceptionOnCreateOneToManyWithIdentity()
{
$this->setExpectedException('Doctrine\ORM\Mapping\MappingException');
$this->builder->createOneToMany('groups', 'Doctrine\Tests\Models\CMS\CmsGroup')
->isPrimaryKey()
->mappedBy('test')
->setOrderBy(array('test'))
->setIndexBy('test')
->build();
}
public function assertIsFluent($ret)
{
$this->assertSame($this->builder, $ret, "Return Value has to be same instance as used builder");