Skip to content

Commit 5bdc542

Browse files
author
Valentin Karnauhov
committed
Fix unnecessary revision creation when only a column from the globalIgnoreColumns list is updated
1 parent 1c5c914 commit 5bdc542

3 files changed

Lines changed: 137 additions & 0 deletions

File tree

src/EventListener/LogRevisionsListener.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,10 @@ private function prepareUpdateData(EntityManagerInterface $em, EntityPersister $
724724
continue;
725725
}
726726

727+
if (\in_array($field, $this->config->getGlobalIgnoreColumns(), true)) {
728+
continue;
729+
}
730+
727731
$newVal = $change[1];
728732

729733
if (!isset($classMetadata->associationMappings[$field])) {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Sonata Project package.
7+
*
8+
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Sonata\EntityAuditBundle\Tests\Fixtures\Issue;
15+
16+
use Doctrine\DBAL\Types\Types;
17+
use Doctrine\ORM\Mapping as ORM;
18+
19+
/**
20+
* @psalm-suppress ClassMustBeFinal
21+
*/
22+
#[ORM\Entity]
23+
class IssueGlobalIgnoreColumnsEntity
24+
{
25+
/**
26+
* @var int|null
27+
*/
28+
#[ORM\Id]
29+
#[ORM\Column(type: Types::INTEGER)]
30+
#[ORM\GeneratedValue]
31+
private $id;
32+
33+
#[ORM\Column(type: Types::STRING)]
34+
private ?string $ignoreme = null;
35+
36+
#[ORM\Column(type: Types::STRING)]
37+
private ?string $name = null;
38+
39+
public function getId(): ?int
40+
{
41+
return $this->id;
42+
}
43+
44+
public function getIgnoreme(): ?string
45+
{
46+
return $this->ignoreme;
47+
}
48+
49+
public function setIgnoreme(?string $ignoreme): self
50+
{
51+
$this->ignoreme = $ignoreme;
52+
53+
return $this;
54+
}
55+
56+
public function getName(): ?string
57+
{
58+
return $this->name;
59+
}
60+
61+
public function setName(?string $name): self
62+
{
63+
$this->name = $name;
64+
65+
return $this;
66+
}
67+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Sonata Project package.
7+
*
8+
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Sonata\EntityAuditBundle\Tests\Issue;
15+
16+
use Sonata\EntityAuditBundle\Tests\BaseTestCase;
17+
use Sonata\EntityAuditBundle\Tests\Fixtures\Issue\IssueGlobalIgnoreColumnsEntity;
18+
19+
final class IssueGlobalIgnoreColumns extends BaseTestCase
20+
{
21+
protected $schemaEntities = [
22+
IssueGlobalIgnoreColumnsEntity::class,
23+
];
24+
25+
protected $auditedEntities = [
26+
IssueGlobalIgnoreColumnsEntity::class,
27+
];
28+
29+
public function testGlobalIgnoreColumns(): void
30+
{
31+
$entity = new IssueGlobalIgnoreColumnsEntity();
32+
$entity->setIgnoreme('test1')
33+
->setName('name1');
34+
35+
$em = $this->getEntityManager();
36+
37+
$em->persist($entity);
38+
$em->flush();
39+
$em->clear();
40+
41+
$entityId = $entity->getId();
42+
static::assertNotNull($entityId);
43+
44+
$persistedEntity = $em->find(IssueGlobalIgnoreColumnsEntity::class, $entityId);
45+
static::assertNotNull($persistedEntity);
46+
47+
$this->getAuditManager();
48+
$cnt = $em->getConnection()->executeQuery('SELECT COUNT(*) FROM revisions')->fetchOne();
49+
50+
$entity = $em->find(IssueGlobalIgnoreColumnsEntity::class, $entityId);
51+
$entity->setIgnoreme('test2')->setName('name2');
52+
$em->flush();
53+
$em->clear();
54+
55+
$newCnt = $em->getConnection()->executeQuery('SELECT COUNT(*) FROM revisions')->fetchOne();
56+
self::assertEquals($cnt + 1, $newCnt);
57+
58+
$entity = $em->find(IssueGlobalIgnoreColumnsEntity::class, $entityId);
59+
$entity->setIgnoreme('test2');
60+
$em->flush();
61+
$em->clear();
62+
63+
$newCnt = $em->getConnection()->executeQuery('SELECT COUNT(*) FROM revisions')->fetchOne();
64+
self::assertEquals($cnt + 1, $newCnt);
65+
}
66+
}

0 commit comments

Comments
 (0)