Skip to content

Commit

Permalink
enhance: Switch to json data field type
Browse files Browse the repository at this point in the history
  • Loading branch information
chapterjason committed Aug 9, 2022
1 parent 70e9681 commit d114aa4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/Loggable/Entity/MappedSuperclass/AbstractLogEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ abstract class AbstractLogEntry
/**
* @var array|null
*
* @ORM\Column(type="array", nullable=true)
* @ORM\Column(type="json", nullable=true)
*/
#[ORM\Column(type: Types::ARRAY, nullable: true)]
#[ORM\Column(type: Types::JSON, nullable: true)]
protected $data;

/**
Expand Down
6 changes: 6 additions & 0 deletions src/Loggable/Entity/Repository/LogEntryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Gedmo\Loggable\Entity\Repository;

use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query;
Expand Down Expand Up @@ -132,6 +133,11 @@ public function revert($entity, $version = 1)
protected function mapValue(ClassMetadata $objectMeta, $field, &$value)
{
if (!$objectMeta->isSingleValuedAssociation($field)) {
$fieldType = $objectMeta->getTypeOfField($field);
$type = Type::getType($fieldType);

$value = $type->convertToPHPValue($value, $this->_em->getConnection()->getDatabasePlatform());

return;
}

Expand Down
31 changes: 19 additions & 12 deletions src/Loggable/LoggableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Gedmo\Loggable;

use Doctrine\Common\EventArgs;
use Doctrine\DBAL\Types\Type;
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs;
use Doctrine\Persistence\ObjectManager;
use Gedmo\Loggable\Mapping\Event\LoggableAdapter;
Expand Down Expand Up @@ -249,20 +250,26 @@ protected function getObjectChangeSetData($ea, $object, $logEntry)
continue;
}
$value = $changes[1];
if ($meta->isSingleValuedAssociation($field) && $value) {
if ($wrapped->isEmbeddedAssociation($field)) {
$value = $this->getObjectChangeSetData($ea, $value, $logEntry);
} else {
$oid = spl_object_id($value);
$wrappedAssoc = AbstractWrapper::wrap($value, $om);
$value = $wrappedAssoc->getIdentifier(false);
if (!is_array($value) && !$value) {
$this->pendingRelatedObjects[$oid][] = [
'log' => $logEntry,
'field' => $field,
];
if ($meta->isSingleValuedAssociation($field)) {
if ($value) {
if ($wrapped->isEmbeddedAssociation($field)) {
$value = $this->getObjectChangeSetData($ea, $value, $logEntry);
} else {
$oid = spl_object_id($value);
$wrappedAssoc = AbstractWrapper::wrap($value, $om);
$value = $wrappedAssoc->getIdentifier(false);
if (!is_array($value) && !$value) {
$this->pendingRelatedObjects[$oid][] = [
'log' => $logEntry,
'field' => $field,
];
}
}
}
} else {
$typeName = $meta->getTypeOfField($field);
$type = Type::getType($typeName);
$value = $type->convertToDatabaseValue($value, $om->getConnection()->getDatabasePlatform());
}
$newValues[$field] = $value;
}
Expand Down

0 comments on commit d114aa4

Please sign in to comment.