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

Fix check for associations declared in MappedSuperclasses #9702

Closed
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: 6 additions & 5 deletions lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,12 @@ private function addInheritedFields(ClassMetadata $subClass, ClassMetadata $pare
private function addInheritedRelations(ClassMetadata $subClass, ClassMetadata $parentClass): void
{
foreach ($parentClass->associationMappings as $field => $mapping) {
if ($parentClass->isMappedSuperclass) {
if (! isset($mapping['declared'])) {
$mapping['declared'] = $parentClass->name;
}

$declaredInParent = $mapping['declared'] === $parentClass->name;
if ($parentClass->isMappedSuperclass && $declaredInParent) {
if ($mapping['type'] & ClassMetadata::TO_MANY && ! $mapping['isOwningSide']) {
throw MappingException::illegalToManyAssociationOnMappedSuperclass($parentClass->name, $field);
}
Expand All @@ -390,10 +395,6 @@ private function addInheritedRelations(ClassMetadata $subClass, ClassMetadata $p
$mapping['inherited'] = $parentClass->name;
}

if (! isset($mapping['declared'])) {
$mapping['declared'] = $parentClass->name;
}

$subClass->addInheritedAssociationMapping($mapping);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\JoinedInheritanceTypeWithAssociation\InvalidToManyOnMappedSuperclass;

use Doctrine\ORM\Mapping\MappedSuperclass;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\Tests\Models\JoinedInheritanceTypeWithAssociation\RootEntity;

/**
* @MappedSuperclass
*/
abstract class ChildMappedSuperclass extends RootEntity
{
/**
* @var InvalidAssociatedEntity
* @OneToMany(targetEntity=InvalidAssociatedEntity::class, mappedBy="childMappedSuperclass")
*/
private $invalidToManyAssociation;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\JoinedInheritanceTypeWithAssociation\InvalidToManyOnMappedSuperclass;

use Doctrine\ORM\Mapping\MappedSuperclass;

/**
* @MappedSuperclass
*/
abstract class GrandchildMappedSuperclass extends ChildMappedSuperclass
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\JoinedInheritanceTypeWithAssociation\InvalidToManyOnMappedSuperclass;

use Doctrine\ORM\Mapping\Entity;

/**
* @Entity
*/
class GreatGrandchildEntity extends GrandchildMappedSuperclass
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\JoinedInheritanceTypeWithAssociation\InvalidToManyOnMappedSuperclass;

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

/**
* @Entity
*/
class InvalidAssociatedEntity
{
/**
* @var int
* @Column(type="integer")
* @Id
* @GeneratedValue
*/
public $id;

/**
* @var ChildMappedSuperclass
* @ManyToOne(targetEntity=ChildMappedSuperclass::class, inversedBy="invalidToManyAssociation")
*/
private $childMappedSuperclass;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\JoinedInheritanceTypeWithAssociation;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\DiscriminatorMap;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\InheritanceType;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\Tests\Models\JoinedInheritanceTypeWithAssociation\ValidToManyOnRoot\AssociatedEntity;

/**
* @DiscriminatorMap({
* "validToManyOnRoot" = ValidToManyOnRoot\GrandchildEntity::class,
* "invalidToManyOnMappedSuperclass" = InvalidToManyOnMappedSuperclass\GreatGrandchildEntity::class,
* "toOneOnMappedSuperclass" = ToOneOnMappedSuperclass\GreatGrandchildEntity::class
* })
* @Entity
* @InheritanceType("JOINED")
*/
class RootEntity
{
/**
* @var int
* @Column(type="integer")
* @Id
* @GeneratedValue
*/
public $id;

/**
* @var AssociatedEntity
* @OneToMany(targetEntity=AssociatedEntity::class, mappedBy="root")
*/
public $toManyAssociation;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\JoinedInheritanceTypeWithAssociation\ToOneOnMappedSuperclass;

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

/**
* @Entity
*/
class AssociatedEntity
{
/**
* @var int
* @Column(type="integer")
* @Id
* @GeneratedValue
*/
public $id;

/**
* @var ChildMappedSuperclass
* @OneToOne(targetEntity=ChildMappedSuperclass::class, inversedBy="toOneAssociation")
*/
private $childMappedSuperclass;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\JoinedInheritanceTypeWithAssociation\ToOneOnMappedSuperclass;

use Doctrine\ORM\Mapping\MappedSuperclass;
use Doctrine\ORM\Mapping\OneToOne;
use Doctrine\Tests\Models\JoinedInheritanceTypeWithAssociation\RootEntity;

/**
* @MappedSuperclass
*/
abstract class ChildMappedSuperclass extends RootEntity
{
/**
* @var AssociatedEntity
* @OneToOne(targetEntity=AssociatedEntity::class, mappedBy="childMappedSuperclass")
*/
private $toOneAssociation;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\JoinedInheritanceTypeWithAssociation\ToOneOnMappedSuperclass;

use Doctrine\ORM\Mapping\MappedSuperclass;

/**
* @MappedSuperclass
*/
abstract class GrandchildMappedSuperclass extends ChildMappedSuperclass
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\JoinedInheritanceTypeWithAssociation\ToOneOnMappedSuperclass;

use Doctrine\ORM\Mapping\Entity;

/**
* @Entity
*/
class GreatGrandchildEntity extends GrandchildMappedSuperclass
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\JoinedInheritanceTypeWithAssociation\ValidToManyOnRoot;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\Tests\Models\JoinedInheritanceTypeWithAssociation\RootEntity;

/**
* @Entity
*/
class AssociatedEntity
{
/**
* @var int
* @Column(type="integer")
* @Id
* @GeneratedValue
*/
public $id;

/**
* @var RootEntity
* @ManyToOne(targetEntity=RootEntity::class, inversedBy="toManyAssociation")
*/
private $root;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\JoinedInheritanceTypeWithAssociation\ValidToManyOnRoot;

use Doctrine\ORM\Mapping\MappedSuperclass;
use Doctrine\Tests\Models\JoinedInheritanceTypeWithAssociation\RootEntity;

/**
* @MappedSuperclass
*/
abstract class ChildMappedSuperclass extends RootEntity
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\JoinedInheritanceTypeWithAssociation\ValidToManyOnRoot;

use Doctrine\ORM\Mapping\Entity;

/**
* @Entity
*/
class GrandchildEntity extends ChildMappedSuperclass
{
}
45 changes: 45 additions & 0 deletions tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
use Doctrine\Tests\Models\JoinedInheritanceType\AnotherChildClass;
use Doctrine\Tests\Models\JoinedInheritanceType\ChildClass;
use Doctrine\Tests\Models\JoinedInheritanceType\RootClass;
use Doctrine\Tests\Models\JoinedInheritanceTypeWithAssociation\InvalidToManyOnMappedSuperclass;
use Doctrine\Tests\Models\JoinedInheritanceTypeWithAssociation\RootEntity;
use Doctrine\Tests\Models\JoinedInheritanceTypeWithAssociation\ToOneOnMappedSuperclass;
use Doctrine\Tests\Models\JoinedInheritanceTypeWithAssociation\ValidToManyOnRoot;
use Doctrine\Tests\Models\Quote;
use Doctrine\Tests\OrmTestCase;
use DoctrineGlobalArticle;
Expand Down Expand Up @@ -151,6 +155,47 @@ public function testGetMetadataForThrowsExceptionOnMissingCustomGeneratorDefinit
$actual = $cmf->getMetadataFor($cm1->name);
}

public function testGetMetadataForJoinedSuperclassWithToManyAssociationAndChildMappedSuperclass(): void
{
$cmf = new ClassMetadataFactory();
$driver = $this->createAnnotationDriver([__DIR__ . '/../../Models/JoinedInheritanceType/']);
$em = $this->createEntityManager($driver);
$cmf->setEntityManager($em);

$rootMetadata = $cmf->getMetadataFor(RootEntity::class);
$childMetadata = $cmf->getMetadataFor(ValidToManyOnRoot\ChildMappedSuperclass::class);
$grandchildMetadata = $cmf->getMetadataFor(ValidToManyOnRoot\GrandchildEntity::class);

self::assertTrue($rootMetadata->hasAssociation('toManyAssociation'));
self::assertTrue($childMetadata->hasAssociation('toManyAssociation'));
self::assertTrue($grandchildMetadata->hasAssociation('toManyAssociation'));
}

public function testThrowsExceptionForToManyOnGrandparentMappedSuperclass(): void
{
$cmf = new ClassMetadataFactory();
$driver = $this->createAnnotationDriver([__DIR__ . '/../../Models/JoinedInheritanceType/']);
$em = $this->createEntityManager($driver);
$cmf->setEntityManager($em);

$rootMetadata = $cmf->getMetadataFor(RootEntity::class);
self::expectException(MappingException::class);
$mappedSuperclass1Metadata = $cmf->getMetadataFor(InvalidToManyOnMappedSuperclass\ChildMappedSuperclass::class);
$mappedSuperclass2Metadata = $cmf->getMetadataFor(InvalidToManyOnMappedSuperclass\GrandchildMappedSuperclass::class);
$grandchildMetadata = $cmf->getMetadataFor(InvalidToManyOnMappedSuperclass\GreatGrandchildEntity::class);
}

public function testInheritedNotSetForToOneOnGrandparentMappedSuperclass(): void
{
$cmf = new ClassMetadataFactory();
$driver = $this->createAnnotationDriver([__DIR__ . '/../../Models/JoinedInheritanceType/']);
$em = $this->createEntityManager($driver);
$cmf->setEntityManager($em);

$grandchildMetadata = $cmf->getMetadataFor(ToOneOnMappedSuperclass\GreatGrandchildEntity::class);
self::assertFalse($grandchildMetadata->isInheritedAssociation('toOneAssociation'));
}

public function testHasGetMetadataNamespaceSeparatorIsNotNormalized(): void
{
require_once __DIR__ . '/../../Models/Global/GlobalNamespaceModel.php';
Expand Down