Skip to content
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

Make "targetEntity must not be a mapped superclass" a lazy check #10554

Merged
Merged
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
11 changes: 0 additions & 11 deletions lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ protected function validateRuntimeMetadata($class, $parent)

$class->validateIdentifier();
$class->validateAssociations();
$this->validateAssociationTargets($class);
$class->validateLifecycleCallbacks($this->getReflectionService());

// verify inheritance
Expand Down Expand Up @@ -304,16 +303,6 @@ protected function validateRuntimeMetadata($class, $parent)
}
}

private function validateAssociationTargets(ClassMetadata $class): void
{
foreach ($class->getAssociationMappings() as $mapping) {
$targetEntity = $mapping['targetEntity'];
if ($this->driver->isTransient($targetEntity) || $this->peekIfIsMappedSuperclass($targetEntity)) {
throw MappingException::associationTargetIsNotAnEntity($targetEntity, $class->name, $mapping['fieldName']);
}
}
}

/**
* {@inheritDoc}
*/
Expand Down
9 changes: 0 additions & 9 deletions lib/Doctrine/ORM/Mapping/MappingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -847,15 +847,6 @@ public static function invalidTargetEntityClass($targetEntity, $sourceEntity, $a
return new self('The target-entity ' . $targetEntity . " cannot be found in '" . $sourceEntity . '#' . $associationName . "'.");
}

/**
* @param class-string $targetEntity
* @param class-string $sourceEntity
*/
public static function associationTargetIsNotAnEntity(string $targetEntity, string $sourceEntity, string $associationName): self
{
return new self(sprintf('The target entity class %s specified for %s::$%s is not an entity class.', $targetEntity, $sourceEntity, $associationName));
}

/**
* @param string[] $cascades
* @param string $className
Expand Down
10 changes: 8 additions & 2 deletions lib/Doctrine/ORM/Tools/SchemaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,18 @@ public function validateClass(ClassMetadataInfo $class)
return $ce;
}

$targetMetadata = $cmf->getMetadataFor($assoc['targetEntity']);

if ($targetMetadata->isMappedSuperclass) {
$ce[] = "The target entity '" . $assoc['targetEntity'] . "' specified on " . $class->name . '#' . $fieldName . ' is a mapped superclass. This is not possible since there is no table that a foreign key could refer to.';

return $ce;
}

if ($assoc['mappedBy'] && $assoc['inversedBy']) {
$ce[] = 'The association ' . $class . '#' . $fieldName . ' cannot be defined as both inverse and owning.';
}

$targetMetadata = $cmf->getMetadataFor($assoc['targetEntity']);

if (isset($assoc['id']) && $targetMetadata->containsForeignIdentifier) {
$ce[] = "Cannot map association '" . $class->name . '#' . $fieldName . ' as identifier, because ' .
"the target entity '" . $targetMetadata->name . "' also maps an association as identifier.";
Expand Down
6 changes: 0 additions & 6 deletions tests/Doctrine/Tests/Models/Cache/Attraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping\Cache;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\DiscriminatorMap;
use Doctrine\ORM\Mapping\Entity;
Expand All @@ -30,7 +29,6 @@
* 3 = "Bar"
* })
*/
#[Entity]
abstract class Attraction
{
/**
Expand Down Expand Up @@ -111,8 +109,4 @@ public function addInfo(AttractionInfo $info): void
$this->infos->add($info);
}
}

public static function loadMetadata(ClassMetadata $metadata): void
{
}
}
6 changes: 0 additions & 6 deletions tests/Doctrine/Tests/Models/Cache/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping\Cache;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
Expand All @@ -22,7 +21,6 @@
* @Table("cache_state")
* @Cache("NONSTRICT_READ_WRITE")
*/
#[Entity]
class State
{
/**
Expand Down Expand Up @@ -107,8 +105,4 @@ public function addCity(City $city): void
{
$this->cities[] = $city;
}

public static function loadMetadata(ClassMetadata $metadata): void
{
}
}
6 changes: 0 additions & 6 deletions tests/Doctrine/Tests/Models/Cache/Travel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping\Cache;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
Expand All @@ -24,7 +23,6 @@
* @Entity
* @Table("cache_travel")
*/
#[Entity]
class Travel
{
/**
Expand Down Expand Up @@ -106,8 +104,4 @@ public function getCreatedAt(): DateTime
{
return $this->createdAt;
}

public static function loadMetadata(ClassMetadata $metadata): void
{
}
}
5 changes: 0 additions & 5 deletions tests/Doctrine/Tests/Models/DDC3579/DDC3579Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
Expand Down Expand Up @@ -68,8 +67,4 @@ public function getAdmins(): Collection
{
return $this->admins;
}

public static function loadMetadata(ClassMetadata $metadata): void
{
}
}
5 changes: 0 additions & 5 deletions tests/Doctrine/Tests/Models/DDC5934/DDC5934Member.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\ClassMetadata;

/** @ORM\Entity() */
#[ORM\Entity]
Expand All @@ -24,8 +23,4 @@ public function __construct()
{
$this->contracts = new ArrayCollection();
}

public static function loadMetadata(ClassMetadata $metadata): void
{
}
}
6 changes: 0 additions & 6 deletions tests/Doctrine/Tests/Models/DDC964/DDC964Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

namespace Doctrine\Tests\Models\DDC964;

use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;

/** @Entity */
#[Entity]
class DDC964Address
{
/**
Expand Down Expand Up @@ -102,8 +100,4 @@ public function setStreet(string $street): void
{
$this->street = $street;
}

public static function loadMetadata(ClassMetadata $metadata): void
{
}
}
6 changes: 0 additions & 6 deletions tests/Doctrine/Tests/Models/DDC964/DDC964Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
namespace Doctrine\Tests\Models\DDC964;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\ManyToMany;

/** @Entity */
#[Entity]
class DDC964Group
{
/**
Expand Down Expand Up @@ -62,8 +60,4 @@ public function getUsers(): ArrayCollection
{
return $this->users;
}

public static function loadMetadata(ClassMetadata $metadata): void
{
}
}
29 changes: 0 additions & 29 deletions tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\InheritanceType;
use Doctrine\ORM\Mapping\ManyToMany;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\MappedSuperclass;
use Doctrine\ORM\Mapping\MappingException;
Expand Down Expand Up @@ -163,24 +162,6 @@ public function testJoinTablesWithMappedSuperclassForAnnotationDriver(): void
self::assertEquals(Directory::class, $classDirectory->associationMappings['parentDirectory']['sourceEntity']);
}

/** @group DDC-945 */
public function testInvalidMappedSuperClassWithManyToManyAssociation(): void
{
$annotationDriver = $this->loadDriver();

$em = $this->getTestEntityManager();
$em->getConfiguration()->setMetadataDriverImpl($annotationDriver);
$factory = new ClassMetadataFactory();
$factory->setEntityManager($em);

$this->expectException(MappingException::class);
$this->expectExceptionMessage(
'The target entity class Doctrine\Tests\ORM\Mapping\InvalidMappedSuperClass specified for Doctrine\Tests\ORM\Mapping\InvalidMappedSuperClass::$selfWhatever is not an entity class.'
);

$factory->getMetadataFor(InvalidMappedSuperClass::class);
}

/** @group DDC-1050 */
public function testInvalidMappedSuperClassWithInheritanceInformation(): void
{
Expand Down Expand Up @@ -304,16 +285,6 @@ class ColumnWithoutType
public $id;
}

/** @MappedSuperclass */
class InvalidMappedSuperClass
{
/**
* @psalm-var Collection<int, self>
* @ManyToMany(targetEntity="InvalidMappedSuperClass", mappedBy="invalid")
*/
private $selfWhatever;
}

/**
* @MappedSuperclass
* @InheritanceType("JOINED")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ class MappedSuperclassBase
private $transient;
}

/** @Entity */
class MappedSuperclassRelated1
{
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading