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

ORM 3 compat fixes #2709

Merged
merged 3 commits into from
Oct 17, 2023
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
15 changes: 15 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,16 @@ parameters:
count: 2
path: src/Tool/Wrapper/MongoDocumentWrapper.php

-
message: "#^Access to an undefined property Gedmo\\\\Translatable\\\\Hydrator\\\\ORM\\\\ObjectHydrator\\:\\:\\$em\\.$#"
count: 1
path: src/Translatable/Hydrator/ORM/ObjectHydrator.php

-
message: "#^Access to an undefined property Gedmo\\\\Translatable\\\\Hydrator\\\\ORM\\\\SimpleObjectHydrator\\:\\:\\$em\\.$#"
count: 1
path: src/Translatable/Hydrator/ORM/SimpleObjectHydrator.php

-
message: "#^Access to offset 'association' on an unknown class Doctrine\\\\ODM\\\\MongoDB\\\\Mapping\\\\FieldMapping\\.$#"
count: 2
Expand Down Expand Up @@ -450,6 +460,11 @@ parameters:
count: 2
path: src/Tree/Entity/Repository/NestedTreeRepository.php

-
message: "#^Access to an undefined property Gedmo\\\\Tree\\\\Hydrator\\\\ORM\\\\TreeObjectHydrator\\:\\:\\$em\\.$#"
count: 1
path: src/Tree/Hydrator/ORM/TreeObjectHydrator.php

-
message: "#^Call to an undefined method Doctrine\\\\Persistence\\\\Mapping\\\\ClassMetadata\\:\\:getFieldMapping\\(\\)\\.$#"
count: 6
Expand Down
14 changes: 7 additions & 7 deletions src/Loggable/Entity/Repository/LogEntryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function getLogEntries($entity)
*/
public function getLogEntriesQuery($entity)
{
$wrapped = new EntityWrapper($entity, $this->_em);
$wrapped = new EntityWrapper($entity, $this->getEntityManager());
$objectClass = $wrapped->getMetadata()->getName();
$meta = $this->getClassMetadata();
$dql = "SELECT log FROM {$meta->getName()} log";
Expand All @@ -76,7 +76,7 @@ public function getLogEntriesQuery($entity)
$dql .= ' ORDER BY log.version DESC';

$objectId = (string) $wrapped->getIdentifier(false, true);
$q = $this->_em->createQuery($dql);
$q = $this->getEntityManager()->createQuery($dql);
$q->setParameters([
'objectId' => $objectId,
'objectClass' => $objectClass,
Expand All @@ -102,7 +102,7 @@ public function getLogEntriesQuery($entity)
*/
public function revert($entity, $version = 1)
{
$wrapped = new EntityWrapper($entity, $this->_em);
$wrapped = new EntityWrapper($entity, $this->getEntityManager());
$objectMeta = $wrapped->getMetadata();
$objectClass = $objectMeta->getName();
$meta = $this->getClassMetadata();
Expand All @@ -113,14 +113,14 @@ public function revert($entity, $version = 1)
$dql .= ' ORDER BY log.version DESC';

$objectId = (string) $wrapped->getIdentifier(false, true);
$q = $this->_em->createQuery($dql);
$q = $this->getEntityManager()->createQuery($dql);
$q->setParameters([
'objectId' => $objectId,
'objectClass' => $objectClass,
'version' => $version,
]);

$config = $this->getLoggableListener()->getConfiguration($this->_em, $objectMeta->getName());
$config = $this->getLoggableListener()->getConfiguration($this->getEntityManager(), $objectMeta->getName());
$fields = $config['versioned'];
$filled = false;
$logsFound = false;
Expand Down Expand Up @@ -167,7 +167,7 @@ protected function mapValue(ClassMetadata $objectMeta, $field, &$value)
}

$mapping = $objectMeta->getAssociationMapping($field);
$value = $value ? $this->_em->getReference($mapping['targetEntity'], $value) : null;
$value = $value ? $this->getEntityManager()->getReference($mapping['targetEntity'], $value) : null;
}

/**
Expand All @@ -180,7 +180,7 @@ protected function mapValue(ClassMetadata $objectMeta, $field, &$value)
private function getLoggableListener(): LoggableListener
{
if (null === $this->listener) {
foreach ($this->_em->getEventManager()->getAllListeners() as $listeners) {
foreach ($this->getEntityManager()->getEventManager()->getAllListeners() as $listeners) {
foreach ($listeners as $listener) {
if ($listener instanceof LoggableListener) {
$this->listener = $listener;
Expand Down
2 changes: 1 addition & 1 deletion src/Sortable/Entity/Repository/SortableRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function __construct(EntityManagerInterface $em, ClassMetadata $class)

$this->listener = $sortableListener;
$this->meta = $this->getClassMetadata();
$this->config = $this->listener->getConfiguration($this->_em, $this->meta->getName());
$this->config = $this->listener->getConfiguration($this->getEntityManager(), $this->meta->getName());
}

/**
Expand Down
31 changes: 31 additions & 0 deletions src/Tool/ORM/Hydration/EntityManagerRetriever.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the Doctrine Behavioral Extensions package.
* (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gedmo\Tool\ORM\Hydration;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Internal\Hydration\AbstractHydrator;

/**
* Helper method to retrieve the entity manager for ORM hydrator classes.
*
* This trait includes a compatibility layer for the renamed `Doctrine\ORM\Internal\Hydration\AbstractHydrator::$_em`
* property between ORM 2.x and 3.x.
*
* @mixin AbstractHydrator
*
* @internal
*/
trait EntityManagerRetriever
{
protected function getEntityManager(): EntityManagerInterface
{
return property_exists($this, '_em') ? $this->_em : $this->em;
}
}
30 changes: 15 additions & 15 deletions src/Translatable/Entity/Repository/TranslationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ public function __construct(EntityManagerInterface $em, ClassMetadata $class)
*/
public function translate($entity, $field, $locale, $value)
{
$meta = $this->_em->getClassMetadata(get_class($entity));
$meta = $this->getEntityManager()->getClassMetadata(get_class($entity));
$listener = $this->getTranslatableListener();
$config = $listener->getConfiguration($this->_em, $meta->getName());
$config = $listener->getConfiguration($this->getEntityManager(), $meta->getName());
if (!isset($config['fields']) || !in_array($field, $config['fields'], true)) {
throw new InvalidArgumentException("Entity: {$meta->getName()} does not translate field - {$field}");
}
$needsPersist = true;
if ($locale === $listener->getTranslatableLocale($entity, $meta, $this->getEntityManager())) {
$meta->getReflectionProperty($field)->setValue($entity, $value);
$this->_em->persist($entity);
$this->getEntityManager()->persist($entity);
} else {
if (isset($config['translationClass'])) {
$class = $config['translationClass'];
Expand All @@ -82,7 +82,7 @@ public function translate($entity, $field, $locale, $value)
}
$foreignKey = $meta->getReflectionProperty($meta->getSingleIdentifierFieldName())->getValue($entity);
$objectClass = $config['useObjectClass'];
$transMeta = $this->_em->getClassMetadata($class);
$transMeta = $this->getEntityManager()->getClassMetadata($class);
$trans = $this->findOneBy([
'locale' => $locale,
'objectClass' => $objectClass,
Expand All @@ -102,11 +102,11 @@ public function translate($entity, $field, $locale, $value)
$needsPersist = $listener->getPersistDefaultLocaleTranslation();
}
$type = Type::getType($meta->getTypeOfField($field));
$transformed = $type->convertToDatabaseValue($value, $this->_em->getConnection()->getDatabasePlatform());
$transformed = $type->convertToDatabaseValue($value, $this->getEntityManager()->getConnection()->getDatabasePlatform());
$transMeta->getReflectionProperty('content')->setValue($trans, $transformed);
if ($needsPersist) {
if ($this->_em->getUnitOfWork()->isInIdentityMap($entity)) {
$this->_em->persist($trans);
if ($this->getEntityManager()->getUnitOfWork()->isInIdentityMap($entity)) {
$this->getEntityManager()->persist($trans);
} else {
$oid = spl_object_id($entity);
$listener->addPendingTranslationInsert($oid, $trans);
Expand All @@ -128,12 +128,12 @@ public function translate($entity, $field, $locale, $value)
public function findTranslations($entity)
{
$result = [];
$wrapped = new EntityWrapper($entity, $this->_em);
$wrapped = new EntityWrapper($entity, $this->getEntityManager());
if ($wrapped->hasValidIdentifier()) {
$entityId = $wrapped->getIdentifier();
$config = $this
->getTranslatableListener()
->getConfiguration($this->_em, $wrapped->getMetadata()->getName());
->getConfiguration($this->getEntityManager(), $wrapped->getMetadata()->getName());

if (!$config) {
return $result;
Expand All @@ -144,7 +144,7 @@ public function findTranslations($entity)

$translationClass = $config['translationClass'] ?? $translationMeta->rootEntityName;

$qb = $this->_em->createQueryBuilder();
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('trans.content, trans.field, trans.locale')
->from($translationClass, 'trans')
->where('trans.foreignKey = :entityId', 'trans.objectClass = :entityClass')
Expand Down Expand Up @@ -178,14 +178,14 @@ public function findTranslations($entity)
public function findObjectByTranslatedField($field, $value, $class)
{
$entity = null;
$meta = $this->_em->getClassMetadata($class);
$meta = $this->getEntityManager()->getClassMetadata($class);
$translationMeta = $this->getClassMetadata(); // table inheritance support
if ($meta->hasField($field)) {
$dql = "SELECT trans.foreignKey FROM {$translationMeta->rootEntityName} trans";
$dql .= ' WHERE trans.objectClass = :class';
$dql .= ' AND trans.field = :field';
$dql .= ' AND trans.content = :value';
$q = $this->_em->createQuery($dql);
$q = $this->getEntityManager()->createQuery($dql);
$q->setParameters([
'class' => $class,
'field' => $field,
Expand All @@ -195,7 +195,7 @@ public function findObjectByTranslatedField($field, $value, $class)
$id = $q->getSingleScalarResult();

if (null !== $id) {
$entity = $this->_em->find($class, $id);
$entity = $this->getEntityManager()->find($class, $id);
}
}

Expand All @@ -215,7 +215,7 @@ public function findTranslationsByObjectId($id)
$result = [];
if ($id) {
$translationMeta = $this->getClassMetadata(); // table inheritance support
$qb = $this->_em->createQueryBuilder();
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('trans.content, trans.field, trans.locale')
->from($translationMeta->rootEntityName, 'trans')
->where('trans.foreignKey = :entityId')
Expand All @@ -239,7 +239,7 @@ public function findTranslationsByObjectId($id)
private function getTranslatableListener(): TranslatableListener
{
if (null === $this->listener) {
foreach ($this->_em->getEventManager()->getAllListeners() as $listeners) {
foreach ($this->getEntityManager()->getEventManager()->getAllListeners() as $listeners) {
foreach ($listeners as $listener) {
if ($listener instanceof TranslatableListener) {
return $this->listener = $listener;
Expand Down
5 changes: 4 additions & 1 deletion src/Translatable/Hydrator/ORM/ObjectHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Doctrine\ORM\Internal\Hydration\ObjectHydrator as BaseObjectHydrator;
use Gedmo\Exception\RuntimeException;
use Gedmo\Tool\ORM\Hydration\EntityManagerRetriever;
use Gedmo\Translatable\TranslatableListener;

/**
Expand All @@ -25,6 +26,8 @@
*/
class ObjectHydrator extends BaseObjectHydrator
{
use EntityManagerRetriever;

/**
* State of skipOnLoad for listener between hydrations
*
Expand Down Expand Up @@ -65,7 +68,7 @@ protected function cleanup()
*/
protected function getTranslatableListener()
{
foreach ($this->_em->getEventManager()->getAllListeners() as $listeners) {
foreach ($this->getEntityManager()->getEventManager()->getAllListeners() as $listeners) {
foreach ($listeners as $listener) {
if ($listener instanceof TranslatableListener) {
return $listener;
Expand Down
5 changes: 4 additions & 1 deletion src/Translatable/Hydrator/ORM/SimpleObjectHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Doctrine\ORM\Internal\Hydration\SimpleObjectHydrator as BaseSimpleObjectHydrator;
use Gedmo\Exception\RuntimeException;
use Gedmo\Tool\ORM\Hydration\EntityManagerRetriever;
use Gedmo\Translatable\TranslatableListener;

/**
Expand All @@ -25,6 +26,8 @@
*/
class SimpleObjectHydrator extends BaseSimpleObjectHydrator
{
use EntityManagerRetriever;

/**
* State of skipOnLoad for listener between hydrations
*
Expand Down Expand Up @@ -65,7 +68,7 @@ protected function cleanup()
*/
protected function getTranslatableListener()
{
foreach ($this->_em->getEventManager()->getAllListeners() as $listeners) {
foreach ($this->getEntityManager()->getEventManager()->getAllListeners() as $listeners) {
foreach ($listeners as $listener) {
if ($listener instanceof TranslatableListener) {
return $listener;
Expand Down
4 changes: 2 additions & 2 deletions src/Tree/Entity/Repository/AbstractTreeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function __construct(EntityManagerInterface $em, ClassMetadata $class)
throw new InvalidMappingException('This repository cannot be used for tree type: '.$treeListener->getStrategy($em, $class->getName())->getName());
}

$this->repoUtils = new RepositoryUtils($this->_em, $this->getClassMetadata(), $this->listener, $this);
$this->repoUtils = new RepositoryUtils($this->getEntityManager(), $this->getClassMetadata(), $this->listener, $this);
}

/**
Expand Down Expand Up @@ -98,7 +98,7 @@ public function childCount($node = null, $direct = false)
throw new InvalidArgumentException('Node is not related to this repository');
}

$wrapped = new EntityWrapper($node, $this->_em);
$wrapped = new EntityWrapper($node, $this->getEntityManager());

if (!$wrapped->hasValidIdentifier()) {
throw new InvalidArgumentException('Node is not managed by UnitOfWork');
Expand Down
Loading