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

Implement reference by class names #409

Merged
merged 7 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"ext-sqlite3": "*",
"doctrine/coding-standard": "^10.0",
"doctrine/dbal": "^2.13 || ^3.0",
"doctrine/deprecations": "^1.0",
"doctrine/mongodb-odm": "^1.3.0 || ^2.0.0",
"doctrine/orm": "^2.12",
"phpstan/phpstan": "^1.5",
Expand Down
2 changes: 1 addition & 1 deletion docs/en/how-to/sharing-objects-between-fixtures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ And the ``User`` data loading fixture:
$user->setUsername('jwage');
$user->setPassword('test');
$user->setRole(
$this->getReference('admin-role') // load the stored reference
$this->getReference('admin-role', Role::class) // load the stored reference
);

$manager->persist($user);
Expand Down
38 changes: 32 additions & 6 deletions lib/Doctrine/Common/DataFixtures/AbstractFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\Common\DataFixtures;

use BadMethodCallException;
use Doctrine\Deprecations\Deprecation;

/**
* Abstract Fixture class helps to manage references
Expand Down Expand Up @@ -71,13 +72,27 @@ public function addReference($name, $object)
*
* @see Doctrine\Common\DataFixtures\ReferenceRepository::getReference
*
* @param string $name
* @param string $name
* @param string|null $class
* @psalm-param class-string<T>|null $class
*
* @return object
* @psalm-return $class is null ? object : T
*
* @template T of object
*/
public function getReference($name)
public function getReference($name, $class = null)
greg0ire marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->referenceRepository->getReference($name);
if ($class === null) {
Deprecation::trigger(
'doctrine/data-fixtures',
'https://github.com/doctrine/data-fixtures/pull/409',
'Argument $class of %s() will be mandatory in 2.0.',
__METHOD__
);
}

return $this->referenceRepository->getReference($name, $class);
}

/**
Expand All @@ -86,12 +101,23 @@ public function getReference($name)
*
* @see Doctrine\Common\DataFixtures\ReferenceRepository::hasReference
*
* @param string $name
* @param string $name
* @param string|null $class
* @psalm-param class-string $class
*
* @return bool
*/
public function hasReference($name)
public function hasReference($name, $class = null)
{
return $this->referenceRepository->hasReference($name);
if ($class === null) {
Deprecation::trigger(
'doctrine/data-fixtures',
'https://github.com/doctrine/data-fixtures/pull/409',
'Argument $class of %s() will be mandatory in 2.0.',
__METHOD__
);
}

return $this->referenceRepository->hasReference($name, $class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Doctrine\Common\EventSubscriber;
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;

use function get_class;

/**
* Reference Listener populates identities for
* stored references
Expand Down Expand Up @@ -49,7 +51,7 @@ public function postPersist(LifecycleEventArgs $args)
->getUnitOfWork()
->getDocumentIdentifier($object);

$this->referenceRepository->setReferenceIdentity($name, $identity);
$this->referenceRepository->setReferenceIdentity($name, $identity, get_class($object));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;

use function get_class;

/**
* Reference Listener populates identities for
* stored references
Expand Down Expand Up @@ -50,7 +52,7 @@ public function postPersist(LifecycleEventArgs $args)
->getUnitOfWork()
->getEntityIdentifier($object);

$this->referenceRepository->setReferenceIdentity($name, $identity);
$this->referenceRepository->setReferenceIdentity($name, $identity, get_class($object));
}
}
}
70 changes: 37 additions & 33 deletions lib/Doctrine/Common/DataFixtures/ProxyReferenceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use function file_put_contents;
use function get_class;
use function serialize;
use function substr;
use function unserialize;

/**
Expand All @@ -20,22 +19,6 @@
*/
class ProxyReferenceRepository extends ReferenceRepository
{
/**
* Get real class name of a reference that could be a proxy
*
* @param string $className Class name of reference object
*
* @return string
*/
protected function getRealClass($className)
{
if (substr($className, -5) === 'Proxy') {
return substr($className, 0, -5);
}

return $className;
}

/**
* Serialize reference repository
*
Expand All @@ -53,8 +36,9 @@ public function serialize()
}

return serialize([
'references' => $simpleReferences,
'identities' => $this->getIdentities(),
'references' => $simpleReferences, // For BC, remove in next major.
'identities' => $this->getIdentities(), // For BC, remove in next major.
'identitiesByClass' => $this->getIdentitiesByClass(),
]);
}

Expand All @@ -68,22 +52,42 @@ public function serialize()
public function unserialize($serializedData)
{
$repositoryData = unserialize($serializedData);
$references = $repositoryData['references'];

foreach ($references as $name => $proxyReference) {
$this->setReference(
$name,
$this->getManager()->getReference(
$proxyReference[0], // entity class name
$proxyReference[1] // identifiers
)
);
}

$identities = $repositoryData['identities'];
// For BC, remove in next major.
if (! isset($repositoryData['identitiesByClass'])) {
$references = $repositoryData['references'];

foreach ($references as $name => $proxyReference) {
$this->setReference(
$name,
$this->getManager()->getReference(
$proxyReference[0], // entity class name
$proxyReference[1] // identifiers
)
);
}

$identities = $repositoryData['identities'];

foreach ($identities as $name => $identity) {
$this->setReferenceIdentity($name, $identity);
}

return;
}

foreach ($identities as $name => $identity) {
$this->setReferenceIdentity($name, $identity);
foreach ($repositoryData['identitiesByClass'] as $className => $identities) {
foreach ($identities as $name => $identity) {
$this->setReference(
$name,
$this->getManager()->getReference(
$className,
$identity
)
);

$this->setReferenceIdentity($name, $identity, $className);
}
}
}

Expand Down
Loading