Skip to content

Commit 5ee410d

Browse files
MisatoTremorphansys
andcommitted
Allow composite identifiers in loggable
Composite identifiers with foreign entities Co-authored-by: Javier Spagnoletti <[email protected]>
1 parent 9c46ada commit 5ee410d

36 files changed

+1074
-50
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ a release.
2121
### Added
2222
- Tree: `setSibling()` and `getSibling()` methods in the `Node` interface through the BC `@method` annotation
2323
- Tree: Support array of fields and directions in the `$sortByField` and `$direction` parameters at `AbstractTreeRepository::recover()`
24+
- Loggable: Support for composite identifiers.
2425

2526
### Changed
2627
- Named arguments have precedence over the values passed in the `$data` array in annotation classes at `Gedmo\Mapping\Annotation\`
@@ -34,6 +35,8 @@ a release.
3435

3536
### Deprecated
3637
- Tree: Not implementing `Node` interface in classes that are used as nodes
38+
- Implementing the `Gedmo\Tool\WrapperInterface::getIdentifier()` method without the second argument (`$flatten`) is deprecated and will
39+
be required in version 4.0.
3740

3841
## [3.11.1] - 2023-02-20
3942
### Fixed

phpstan-baseline.neon

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ parameters:
6565
count: 4
6666
path: src/Loggable/LoggableListener.php
6767

68+
-
69+
message: "#^Method Gedmo\\\\Tool\\\\WrapperInterface\\<Doctrine\\\\Persistence\\\\Mapping\\\\ClassMetadata\\>\\:\\:getIdentifier\\(\\) invoked with 2 parameters, 0-1 required\\.#"
70+
count: 2
71+
path: src/Loggable/LoggableListener.php
72+
6873
-
6974
message: "#^Access to an undefined property Doctrine\\\\Persistence\\\\Mapping\\\\ClassMetadata\\:\\:\\$associationMappings\\.$#"
7075
count: 1
@@ -355,11 +360,26 @@ parameters:
355360
count: 1
356361
path: src/Timestampable/Mapping/Driver/Yaml.php
357362

363+
-
364+
message: "#^PHPDoc tag \\@param references unknown parameter\\: \\$flatten$#"
365+
count: 1
366+
path: src/Tool/WrapperInterface.php
367+
358368
-
359369
message: "#^Call to an undefined method Doctrine\\\\Persistence\\\\ObjectManager\\:\\:getUnitOfWork\\(\\)\\.$#"
360370
count: 1
361371
path: src/Tool/Wrapper/EntityWrapper.php
362372

373+
-
374+
message: "#^Method Gedmo\\\\Tool\\\\Wrapper\\\\EntityWrapper\\:\\:getIdentifier\\(\\) has parameter \\$flatten with no type specified\\.$#"
375+
count: 1
376+
path: src/Tool/Wrapper/EntityWrapper.php
377+
378+
-
379+
message: "#^Parameter \\#2 \\$em of class Gedmo\\\\Tool\\\\Wrapper\\\\EntityWrapper constructor expects Doctrine\\\\ORM\\\\EntityManagerInterface, Doctrine\\\\Persistence\\\\ObjectManager given\\.$#"
380+
count: 1
381+
path: src/Tool/Wrapper/EntityWrapper.php
382+
363383
-
364384
message: "#^Access to an undefined property ProxyManager\\\\Proxy\\\\GhostObjectInterface\\:\\:\\$identifier\\.$#"
365385
count: 1
@@ -370,6 +390,11 @@ parameters:
370390
count: 2
371391
path: src/Tool/Wrapper/MongoDocumentWrapper.php
372392

393+
-
394+
message: "#^Method Gedmo\\\\Tool\\\\Wrapper\\\\MongoDocumentWrapper\\:\\:getIdentifier\\(\\) has parameter \\$flatten with no type specified\\.$#"
395+
count: 1
396+
path: src/Tool/Wrapper/MongoDocumentWrapper.php
397+
373398
-
374399
message: "#^Access to offset 'association' on an unknown class Doctrine\\\\ODM\\\\MongoDB\\\\Mapping\\\\FieldMapping\\.$#"
375400
count: 2

src/Loggable/Document/Repository/LogEntryRepository.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ public function revert($document, $version = 1)
106106
throw new UnexpectedValueException('Count not find any log entries under version: '.$version);
107107
}
108108

109-
$data = [];
109+
$data = [[]];
110110
while ($log = array_shift($logs)) {
111-
$data = array_merge($data, $log->getData());
111+
$data[] = $log->getData();
112112
}
113+
$data = array_merge(...$data);
113114
$this->fillDocument($document, $data);
114115
}
115116

src/Loggable/Entity/Repository/LogEntryRepository.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function getLogEntriesQuery($entity)
7777
$dql .= ' AND log.objectClass = :objectClass';
7878
$dql .= ' ORDER BY log.version DESC';
7979

80-
$objectId = (string) $wrapped->getIdentifier();
80+
$objectId = (string) $wrapped->getIdentifier(false, true);
8181
$q = $this->_em->createQuery($dql);
8282
$q->setParameters(compact('objectId', 'objectClass'));
8383

@@ -111,7 +111,7 @@ public function revert($entity, $version = 1)
111111
$dql .= ' AND log.version <= :version';
112112
$dql .= ' ORDER BY log.version ASC';
113113

114-
$objectId = (string) $wrapped->getIdentifier();
114+
$objectId = (string) $wrapped->getIdentifier(false, true);
115115
$q = $this->_em->createQuery($dql);
116116
$q->setParameters(compact('objectId', 'objectClass', 'version'));
117117
$logs = $q->getResult();

src/Loggable/LoggableListener.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
namespace Gedmo\Loggable;
1111

1212
use Doctrine\Common\EventArgs;
13+
use Doctrine\ORM\Mapping\ClassMetadata;
1314
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs;
1415
use Doctrine\Persistence\ObjectManager;
1516
use Gedmo\Exception\InvalidArgumentException;
17+
use Gedmo\Loggable\Entity\LogEntry;
1618
use Gedmo\Loggable\Mapping\Event\LoggableAdapter;
1719
use Gedmo\Mapping\MappedEventSubscriber;
1820
use Gedmo\Tool\Wrapper\AbstractWrapper;
@@ -150,7 +152,7 @@ public function postPersist(EventArgs $args)
150152
$logEntry = $this->pendingLogEntryInserts[$oid];
151153
$logEntryMeta = $om->getClassMetadata(get_class($logEntry));
152154

153-
$id = $wrapped->getIdentifier();
155+
$id = $wrapped->getIdentifier(false, true);
154156
$logEntryMeta->getReflectionProperty('objectId')->setValue($logEntry, $id);
155157
$uow->scheduleExtraUpdate($logEntry, [
156158
'objectId' => [null, $id],
@@ -320,10 +322,10 @@ protected function createLogEntry($action, $object, LoggableAdapter $ea)
320322

321323
// check for the availability of the primary key
322324
$uow = $om->getUnitOfWork();
323-
if (LogEntryInterface::ACTION_CREATE === $action && $ea->isPostInsertGenerator($meta)) {
325+
if (LogEntryInterface::ACTION_CREATE === $action && ($ea->isPostInsertGenerator($meta) || ($meta instanceof ClassMetadata && $meta->isIdentifierComposite))) {
324326
$this->pendingLogEntryInserts[spl_object_id($object)] = $logEntry;
325327
} else {
326-
$logEntry->setObjectId($wrapped->getIdentifier());
328+
$logEntry->setObjectId($wrapped->getIdentifier(false, true));
327329
}
328330
$newValues = [];
329331
if (LogEntryInterface::ACTION_REMOVE !== $action && isset($config['versioned'])) {

src/Loggable/Mapping/Driver/Annotation.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Gedmo\Loggable\Mapping\Driver;
1111

12+
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata as ClassMetadataODM;
1213
use Doctrine\Persistence\Mapping\ClassMetadata;
1314
use Gedmo\Exception\InvalidMappingException;
1415
use Gedmo\Mapping\Annotation\Loggable;
@@ -40,7 +41,7 @@ class Annotation extends AbstractAnnotationDriver
4041

4142
public function validateFullMetadata(ClassMetadata $meta, array $config)
4243
{
43-
if ($config && is_array($meta->getIdentifier()) && count($meta->getIdentifier()) > 1) {
44+
if ($config && $meta instanceof ClassMetadataODM && is_array($meta->getIdentifier()) && count($meta->getIdentifier()) > 1) {
4445
throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->getName()}");
4546
}
4647
if (isset($config['versioned']) && !isset($config['loggable'])) {
@@ -87,7 +88,7 @@ public function readExtendedMetadata($meta, array &$config)
8788
}
8889

8990
if (!$meta->isMappedSuperclass && $config) {
90-
if (is_array($meta->getIdentifier()) && count($meta->getIdentifier()) > 1) {
91+
if ($meta instanceof ClassMetadataODM && is_array($meta->getIdentifier()) && count($meta->getIdentifier()) > 1) {
9192
throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->getName()}");
9293
}
9394
if ($this->isClassAnnotationInValid($meta, $config)) {

src/Loggable/Mapping/Driver/Xml.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Gedmo\Loggable\Mapping\Driver;
1111

12+
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata as ClassMetadataODM;
1213
use Doctrine\Persistence\Mapping\ClassMetadata;
1314
use Gedmo\Exception\InvalidMappingException;
1415
use Gedmo\Mapping\Driver\Xml as BaseXml;
@@ -37,7 +38,7 @@ public function readExtendedMetadata($meta, array &$config)
3738

3839
$xml = $xml->children(self::GEDMO_NAMESPACE_URI);
3940

40-
if ('entity' === $xmlDoctrine->getName() || 'document' === $xmlDoctrine->getName() || 'mapped-superclass' === $xmlDoctrine->getName()) {
41+
if (in_array($xmlDoctrine->getName(), ['mapped-superclass', 'entity', 'document'], true)) {
4142
if (isset($xml->loggable)) {
4243
/**
4344
* @var \SimpleXMLElement;
@@ -74,7 +75,7 @@ public function readExtendedMetadata($meta, array &$config)
7475
}
7576

7677
if (!$meta->isMappedSuperclass && $config) {
77-
if (is_array($meta->getIdentifier()) && count($meta->getIdentifier()) > 1) {
78+
if ($meta instanceof ClassMetadataODM && is_array($meta->getIdentifier()) && count($meta->getIdentifier()) > 1) {
7879
throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->getName()}");
7980
}
8081
if (isset($config['versioned']) && !isset($config['loggable'])) {

src/Loggable/Mapping/Driver/Yaml.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Gedmo\Loggable\Mapping\Driver;
1111

12+
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
1213
use Gedmo\Exception\InvalidMappingException;
1314
use Gedmo\Mapping\Driver;
1415
use Gedmo\Mapping\Driver\File;
@@ -26,7 +27,7 @@
2627
*
2728
* @internal
2829
*/
29-
class Yaml extends File implements Driver
30+
class Yaml extends File
3031
{
3132
/**
3233
* File extension
@@ -124,11 +125,11 @@ public function readExtendedMetadata($meta, array &$config)
124125
}
125126

126127
if (!$meta->isMappedSuperclass && $config) {
127-
if (is_array($meta->getIdentifier()) && count($meta->getIdentifier()) > 1) {
128+
if ($meta instanceof ClassMetadata && is_array($meta->getIdentifier()) && count($meta->getIdentifier()) > 1) {
128129
throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->getName()}");
129130
}
130131
if (isset($config['versioned']) && !isset($config['loggable'])) {
131-
throw new InvalidMappingException("Class must be annoted with Loggable annotation in order to track versioned fields in class - {$meta->getName()}");
132+
throw new InvalidMappingException("Class must be annotated with Loggable annotation in order to track versioned fields in class - {$meta->getName()}");
132133
}
133134
}
134135
}

src/Loggable/Mapping/Event/Adapter/ORM.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Gedmo\Loggable\Entity\LogEntry;
1414
use Gedmo\Loggable\Mapping\Event\LoggableAdapter;
1515
use Gedmo\Mapping\Event\Adapter\ORM as BaseAdapterORM;
16+
use Gedmo\Tool\Wrapper\EntityWrapper;
1617

1718
/**
1819
* Doctrine event adapter for ORM adapted
@@ -39,8 +40,8 @@ public function getNewVersion($meta, $object)
3940
{
4041
$em = $this->getObjectManager();
4142
$objectMeta = $em->getClassMetadata(get_class($object));
42-
$identifierField = $this->getSingleIdentifierFieldName($objectMeta);
43-
$objectId = (string) $objectMeta->getReflectionProperty($identifierField)->getValue($object);
43+
$wrapper = new EntityWrapper($object, $em);
44+
$objectId = $wrapper->getIdentifier(false, true);
4445

4546
$dql = "SELECT MAX(log.version) FROM {$meta->getName()} log";
4647
$dql .= ' WHERE log.objectId = :objectId';

src/References/Mapping/Driver/Xml.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function readExtendedMetadata($meta, array &$config)
5151

5252
$xml = $xml->children(self::GEDMO_NAMESPACE_URI);
5353

54-
if ('entity' === $xmlDoctrine->getName() || 'document' === $xmlDoctrine->getName() || 'mapped-superclass' === $xmlDoctrine->getName()) {
54+
if (in_array($xmlDoctrine->getName(), ['mapped-superclass', 'entity', 'document'], true)) {
5555
if (isset($xml->reference)) {
5656
/**
5757
* @var \SimpleXMLElement

0 commit comments

Comments
 (0)