Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
Merge branch 'feature/doctrine#1229-to-one-association-builder'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocramius committed Feb 16, 2015
2 parents b523896 + 1f67218 commit a13143b
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/Doctrine/ORM/Mapping/Builder/AssociationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,18 @@ public function addJoinColumn($columnName, $referencedColumnName, $nullable = tr
return $this;
}

/**
* Sets field as primary key.
*
* @return self
*/
public function isPrimaryKey()
{
$this->mapping['id'] = true;

return $this;
}

/**
* @return ClassMetadataBuilder
*
Expand Down
4 changes: 4 additions & 0 deletions lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,10 @@ protected function _validateAndCompleteAssociationMapping(array $mapping)
}

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']
Expand Down
175 changes: 174 additions & 1 deletion tests/Doctrine/Tests/ORM/Mapping/ClassMetadataBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,73 @@ public function testCreateManyToOne()
), $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(
Expand Down Expand Up @@ -386,11 +453,91 @@ public function testCreateOneToOne()
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(
Expand Down Expand Up @@ -474,6 +621,20 @@ public function testCreateManyToMany()
), $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(
Expand Down Expand Up @@ -513,6 +674,18 @@ public function testCreateOneToMany()
), $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");
Expand Down

0 comments on commit a13143b

Please sign in to comment.