Skip to content

Association Mappings: replace assertions by explicit exceptions #11896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: 3.4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Mapping/AssociationMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,13 @@ public static function fromMappingArray(array $mappingArray): static
continue;
}

assert($mapping instanceof ManyToManyOwningSideMapping);
if (! $mapping instanceof ManyToManyOwningSideMapping) {
throw new MappingException(
"Mapping error on field '" .
$mapping->fieldName . "' in " . $mapping->sourceEntity .
" : 'joinTable' can only be set on many-to-many owning side.",
);
}

$mapping->joinTable = JoinTableMapping::fromMappingArray($value);

Expand Down
133 changes: 133 additions & 0 deletions tests/Tests/ORM/Functional/InvalidMappingDefinitionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\MappingException;
use Doctrine\ORM\Mapping\Table;
use Doctrine\Tests\OrmFunctionalTestCase;

/**
* Functional tests for the Class Table Inheritance mapping strategy.
*/
class InvalidMappingDefinitionTest extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();
}

public function testManyToManyRelationWithJoinTableOnTheWrongSide(): void
{
$this->expectException(MappingException::class);
$this->expectExceptionMessage("Mapping error on field 'owners' in Doctrine\Tests\ORM\Functional\OwnedSideEntity : 'joinTable' can only be set on many-to-many owning side.");

$this->createSchemaForModels(
OwningSideEntity::class,
OwnedSideEntity::class,
);
}
}

#[Table(name: 'owning_side_entities1')]
#[Entity]
class OwningSideEntity
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private int $id;

#[ORM\ManyToMany(targetEntity: OwnedSideEntity::class, inversedBy: 'owners')]
#[ORM\JoinTable(name: 'owning_owned')]
private Collection $relations;

public function __construct()
{
$this->relations = new ArrayCollection();
}

public function getId(): int
{
return $this->id;
}

public function getRelations(): Collection
{
return $this->relations;
}

public function addRelation(OwnedSideEntity $ownedSide): void
{
if (! $this->relations->contains($ownedSide)) {
$this->relations->add($ownedSide);
$ownedSide->addOwner($this);
}
}

public function removeRelation(OwnedSideEntity $ownedSide): void
{
if ($this->relations->removeElement($ownedSide)) {
$ownedSide->removeOwner($this);
}
}
}

#[Table(name: 'owned_side_entities1')]
#[Entity]
class OwnedSideEntity
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private int $id;

#[ORM\Column(type: 'string', length: 255)]
private string $data;

#[ORM\ManyToMany(targetEntity: OwningSideEntity::class, mappedBy: 'relations')]
#[ORM\JoinTable(name: 'owning_owned')]
private Collection $owners;

public function __construct()
{
$this->owners = new ArrayCollection();
}

public function getId(): int
{
return $this->id;
}

public function getData(): string
{
return $this->data;
}

public function setData(string $data): void
{
$this->data = $data;
}

public function getOwners(): Collection
{
return $this->owners;
}

public function addOwner(OwningSideEntity $owningSide): void
{
if (! $this->owners->contains($owningSide)) {
$this->owners->add($owningSide);
}
}

public function removeOwner(OwningSideEntity $owningSide): void
{
$this->owners->removeElement($owningSide);
}
}