1
0
mirror of synced 2025-02-09 00:39:25 +03:00

#5934 - add fetch option to AssociationOverride in order to override fetch strategy for subclasses of entities

This commit is contained in:
Maximilian Bosch 2016-07-16 12:12:33 +02:00 committed by Luís Cobucci
parent 205ee72e33
commit 92476b5953
No known key found for this signature in database
GPG Key ID: EC61C5F01750ED3C
17 changed files with 213 additions and 0 deletions

View File

@ -455,6 +455,7 @@ Things to note:
- The association type *CANNOT* be changed. - The association type *CANNOT* be changed.
- The override could redefine the joinTables or joinColumns depending on the association type. - The override could redefine the joinTables or joinColumns depending on the association type.
- The override could redefine inversedBy to reference more than one extended entity. - The override could redefine inversedBy to reference more than one extended entity.
- The override could redefine fetch to modify the fetch strategy of the extended entity.
Attribute Override Attribute Override
~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~

View File

@ -567,6 +567,7 @@
<xs:any minOccurs="0" maxOccurs="unbounded" namespace="##other"/> <xs:any minOccurs="0" maxOccurs="unbounded" namespace="##other"/>
</xs:sequence> </xs:sequence>
<xs:attribute name="name" type="xs:NMTOKEN" use="required" /> <xs:attribute name="name" type="xs:NMTOKEN" use="required" />
<xs:attribute name="fetch" type="orm:fetch-type" use="optional" />
</xs:complexType> </xs:complexType>
<xs:complexType name="inversed-by-override"> <xs:complexType name="inversed-by-override">

View File

@ -57,4 +57,13 @@ final class AssociationOverride implements Annotation
* @var string * @var string
*/ */
public $inversedBy; public $inversedBy;
/**
* The fetching strategy to use for the association.
*
* @var string
*
* @Enum({"LAZY", "EAGER", "EXTRA_LAZY"})
*/
public $fetch;
} }

View File

@ -2153,6 +2153,10 @@ class ClassMetadataInfo implements ClassMetadata
$mapping['joinTable'] = $overrideMapping['joinTable']; $mapping['joinTable'] = $overrideMapping['joinTable'];
} }
if (isset($overrideMapping['fetch'])) {
$mapping['fetch'] = $overrideMapping['fetch'];
}
$mapping['joinColumnFieldNames'] = null; $mapping['joinColumnFieldNames'] = null;
$mapping['joinTableColumns'] = null; $mapping['joinTableColumns'] = null;
$mapping['sourceToTargetKeyColumns'] = null; $mapping['sourceToTargetKeyColumns'] = null;

View File

@ -470,6 +470,11 @@ class AnnotationDriver extends AbstractAnnotationDriver
$override['inversedBy'] = $associationOverride->inversedBy; $override['inversedBy'] = $associationOverride->inversedBy;
} }
// Check for `fetch`
if ($associationOverride->fetch) {
$override['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $associationOverride->fetch);
}
$metadata->setAssociationOverride($fieldName, $override); $metadata->setAssociationOverride($fieldName, $override);
} }
} }

View File

@ -621,6 +621,11 @@ class XmlDriver extends FileDriver
$override['inversedBy'] = (string) $overrideElement->{'inversed-by'}['name']; $override['inversedBy'] = (string) $overrideElement->{'inversed-by'}['name'];
} }
// Check for `fetch`
if (isset($overrideElement['fetch'])) {
$override['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . (string) $overrideElement['fetch']);
}
$metadata->setAssociationOverride($fieldName, $override); $metadata->setAssociationOverride($fieldName, $override);
} }
} }

View File

@ -622,6 +622,11 @@ class YamlDriver extends FileDriver
$override['inversedBy'] = (string) $associationOverrideElement['inversedBy']; $override['inversedBy'] = (string) $associationOverrideElement['inversedBy'];
} }
// Check for `fetch`
if (isset($associationOverrideElement['fetch'])) {
$override['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $associationOverrideElement['fetch']);
}
$metadata->setAssociationOverride($fieldName, $override); $metadata->setAssociationOverride($fieldName, $override);
} }
} }

View File

@ -0,0 +1,53 @@
<?php
namespace Doctrine\Tests\Models\DDC5934;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\ManyToMany;
/**
* @Entity
*/
class DDC5934BaseContract
{
/**
* @Id()
* @Column(name="id", type="integer")
* @GeneratedValue()
*/
public $id;
/**
* @var ArrayCollection
*
* @ManyToMany(targetEntity="DDC5934Member", fetch="LAZY", inversedBy="contracts")
*/
public $members;
public function __construct()
{
$this->members = new ArrayCollection();
}
public static function loadMetadata(ClassMetadata $metadata)
{
$metadata->mapField([
'id' => true,
'fieldName' => 'id',
'type' => 'integer',
'columnName' => 'id',
]);
$metadata->mapManyToMany([
'fieldName' => 'members',
'targetEntity' => 'DDC5934Member',
]);
$metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO);
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace Doctrine\Tests\Models\DDC5934;
use Doctrine\ORM\Mapping\AssociationOverride;
use Doctrine\ORM\Mapping\AssociationOverrides;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\ClassMetadata;
/**
* @Entity
* @AssociationOverrides(
* @AssociationOverride(name="members", fetch="EXTRA_LAZY")
* )
*/
class DDC5934Contract extends DDC5934BaseContract
{
public static function loadMetadata(ClassMetadata $metadata)
{
$metadata->setAssociationOverride('members', [
'fetch' => ClassMetadata::FETCH_EXTRA_LAZY,
]);
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace Doctrine\Tests\Models\DDC5934;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class DDC5934Member
{
/**
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="DDC5934BaseContract", mappedBy="members")
*/
public $contracts;
public function __construct()
{
$this->contracts = new ArrayCollection();
}
}

View File

@ -28,6 +28,7 @@ use Doctrine\Tests\Models\DDC1476\DDC1476EntityWithDefaultFieldType;
use Doctrine\Tests\Models\DDC2825\ExplicitSchemaAndTable; use Doctrine\Tests\Models\DDC2825\ExplicitSchemaAndTable;
use Doctrine\Tests\Models\DDC2825\SchemaAndTableInTableName; use Doctrine\Tests\Models\DDC2825\SchemaAndTableInTableName;
use Doctrine\Tests\Models\DDC3579\DDC3579Admin; use Doctrine\Tests\Models\DDC3579\DDC3579Admin;
use Doctrine\Tests\Models\DDC5934\DDC5934Contract;
use Doctrine\Tests\Models\DDC869\DDC869ChequePayment; use Doctrine\Tests\Models\DDC869\DDC869ChequePayment;
use Doctrine\Tests\Models\DDC869\DDC869CreditCardPayment; use Doctrine\Tests\Models\DDC869\DDC869CreditCardPayment;
use Doctrine\Tests\Models\DDC869\DDC869PaymentRepository; use Doctrine\Tests\Models\DDC869\DDC869PaymentRepository;
@ -816,6 +817,18 @@ abstract class AbstractMappingDriverTest extends OrmTestCase
$this->assertEquals('admins', $adminGroups['inversedBy']); $this->assertEquals('admins', $adminGroups['inversedBy']);
} }
/**
* @group DDC-5934
*/
public function testFetchOverrideMapping()
{
// check override metadata
$contractMetadata = $this->createClassMetadataFactory()->getMetadataFor(DDC5934Contract::class);
$this->assertArrayHasKey('members', $contractMetadata->associationMappings);
$this->assertSame(ClassMetadata::FETCH_EXTRA_LAZY, $contractMetadata->associationMappings['members']['fetch']);
}
/** /**
* @group DDC-964 * @group DDC-964
*/ */

View File

@ -0,0 +1,17 @@
<?php
use Doctrine\ORM\Mapping\ClassMetadata;
$metadata->mapField([
'id' => true,
'fieldName' => 'id',
'type' => 'integer',
'columnName' => 'id',
]);
$metadata->mapManyToMany([
'fieldName' => 'members',
'targetEntity' => 'DDC5934Member',
]);
$metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO);

View File

@ -0,0 +1,7 @@
<?php
use Doctrine\ORM\Mapping\ClassMetadata;
$metadata->setAssociationOverride('members', [
'fetch' => ClassMetadata::FETCH_EXTRA_LAZY,
]);

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Doctrine\Tests\Models\DDC5934\DDC5934BaseContract">
<id name="id" type="integer">
<generator strategy="AUTO" />
</id>
<many-to-many target-entity="DDC5934Member" inversed-by="contract" fetch="LAZY" field="members" />
</entity>
</doctrine-mapping>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Doctrine\Tests\Models\DDC5934\DDC5934Contract">
<association-overrides>
<association-override name="members" fetch="EXTRA_LAZY" />
</association-overrides>
</entity>
</doctrine-mapping>

View File

@ -0,0 +1,12 @@
Doctrine\Tests\Models\DDC5934\DDC5934BaseContract:
type: mappedSuperclass
id:
id:
type: integer
column: id
generator:
strategy: AUTO
manyToMany:
members:
targetEntity: DDC5934Member
inversedBy: contract

View File

@ -0,0 +1,5 @@
Doctrine\Tests\Models\DDC5934\DDC5934Contract:
type: entity
associationOverride:
members:
fetch: EXTRA_LAZY