Skip to content

Commit 0f4649b

Browse files
committed
Fixed getting property name in annotation drivers
Fixed using setterMethod for SoftDeleteable annotations Added Unit tests for the setter methods Amended Code Style
1 parent 5f8a2bf commit 0f4649b

File tree

11 files changed

+82
-12
lines changed

11 files changed

+82
-12
lines changed

src/Blameable/Mapping/Driver/Annotation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function readExtendedMetadata($meta, array &$config)
8888
];
8989
}
9090
// add the setter method for the field
91-
$this->setSetterMethod($field, $blameable->setterMethod, $config);
91+
$this->setSetterMethod($property->getName(), $blameable->setterMethod, $config);
9292
// properties are unique and mapper checks that, no risk here
9393
$config[$blameable->on][] = $field;
9494
}

src/IpTraceable/Mapping/Driver/Annotation.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public function readExtendedMetadata($meta, array &$config)
4545
// property annotations
4646
foreach ($class->getProperties() as $property) {
4747
if (
48-
isset($meta->associationMappings[$property->name]['inherited']) ||
49-
($meta->isMappedSuperclass && !$property->isPrivate()) ||
50-
$meta->isInheritedField($property->name)
48+
isset($meta->associationMappings[$property->name]['inherited'])
49+
|| ($meta->isMappedSuperclass && !$property->isPrivate())
50+
|| $meta->isInheritedField($property->name)
5151
) {
5252
continue;
5353
}
@@ -79,7 +79,7 @@ public function readExtendedMetadata($meta, array &$config)
7979
];
8080
}
8181
// add the setter method for the field
82-
$this->setSetterMethod($field, $ipTraceable->setterMethod, $config);
82+
$this->setSetterMethod($property->getName(), $ipTraceable->setterMethod, $config);
8383
// properties are unique and mapper checks that, no risk here
8484
$config[$ipTraceable->on][] = $field;
8585
}

src/SoftDeleteable/SoftDeleteableListener.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs;
1818
use Doctrine\Persistence\Mapping\ClassMetadata;
1919
use Doctrine\Persistence\ObjectManager;
20+
use Gedmo\Exception\InvalidMappingException;
2021
use Gedmo\Mapping\MappedEventSubscriber;
2122
use Gedmo\SoftDeleteable\Event\PostSoftDeleteEventArgs;
2223
use Gedmo\SoftDeleteable\Event\PreSoftDeleteEventArgs;
@@ -96,7 +97,18 @@ public function onFlush(EventArgs $args)
9697
);
9798
}
9899

99-
$reflProp->setValue($object, $date);
100+
if (!empty($config['setterMethod'][$config['fieldName']])) {
101+
$reflectionClass = $meta->getReflectionClass();
102+
$setterName = $config['setterMethod'][$config['fieldName']];
103+
104+
if (!$reflectionClass->hasMethod($setterName)) {
105+
throw new InvalidMappingException("Setter method - [{$setterName}] does not exist in class - {$meta->getName()}");
106+
}
107+
108+
$reflectionClass->getMethod($setterName)->invoke($object, $date);
109+
} else {
110+
$reflProp->setValue($object, $date);
111+
}
100112

101113
$om->persist($object);
102114
$uow->propertyChanged($object, $config['fieldName'], $oldValue, $date);

src/Timestampable/Mapping/Driver/Annotation.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ public function readExtendedMetadata($meta, array &$config)
5555
// property annotations
5656
foreach ($class->getProperties() as $property) {
5757
if (
58-
isset($meta->associationMappings[$property->name]['inherited']) ||
59-
($meta->isMappedSuperclass && !$property->isPrivate()) ||
60-
$meta->isInheritedField($property->name)
58+
isset($meta->associationMappings[$property->name]['inherited'])
59+
|| ($meta->isMappedSuperclass && !$property->isPrivate())
60+
|| $meta->isInheritedField($property->name)
6161
) {
6262
continue;
6363
}
@@ -87,7 +87,7 @@ public function readExtendedMetadata($meta, array &$config)
8787
];
8888
}
8989
// add the setter method for the field
90-
$this->setSetterMethod($field, $timestampable->setterMethod, $config);
90+
$this->setSetterMethod($property->getName(), $timestampable->setterMethod, $config);
9191
// properties are unique and mapper checks that, no risk here
9292
$config[$timestampable->on][] = $field;
9393
}

tests/Gedmo/Blameable/BlameableTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function testBlameable(): void
6868
static::assertNull($sport->getPublished());
6969

7070
$sportComment = $this->em->getRepository(self::COMMENT)->findOneBy(['message' => 'hello']);
71+
static::assertSame('testuser', $sportComment->getCreated());
7172
static::assertSame('testuser', $sportComment->getModified());
7273
static::assertNull($sportComment->getClosed());
7374

tests/Gedmo/Blameable/Fixture/Entity/Comment.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ class Comment implements Blameable
5252
#[ORM\Column(type: Types::INTEGER)]
5353
private ?int $status = null;
5454

55+
/**
56+
* @Gedmo\Blameable(on="create")
57+
*
58+
* @ORM\Column(name="created", type="string")
59+
*/
60+
#[ORM\Column(name: 'created', type: Types::STRING)]
61+
#[Gedmo\Blameable(on: 'create', setterMethod: 'setCreated')]
62+
private ?string $created = null;
63+
5564
/**
5665
* @var string|null
5766
*
@@ -104,6 +113,16 @@ public function getMessage(): ?string
104113
return $this->message;
105114
}
106115

116+
public function getCreated(): ?string
117+
{
118+
return $this->created;
119+
}
120+
121+
public function setCreated(?string $created): void
122+
{
123+
$this->created = $created;
124+
}
125+
107126
public function getModified(): ?string
108127
{
109128
return $this->modified;

tests/Gedmo/IpTraceable/Fixture/Comment.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class Comment implements IpTraceable
7171
* @Gedmo\IpTraceable(on="update")
7272
*/
7373
#[ORM\Column(name: 'modified', type: Types::STRING, length: 45)]
74-
#[Gedmo\IpTraceable(on: 'update')]
74+
#[Gedmo\IpTraceable(on: 'update', setterMethod: 'setModified')]
7575
private $modified;
7676

7777
public function setArticle(?Article $article): void
@@ -109,6 +109,11 @@ public function getModified(): ?string
109109
return $this->modified;
110110
}
111111

112+
public function setModified(?string $modified): void
113+
{
114+
$this->modified = $modified;
115+
}
116+
112117
public function getClosed(): ?string
113118
{
114119
return $this->closed;

tests/Gedmo/SoftDeleteable/Fixture/Entity/Comment.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* @Gedmo\SoftDeleteable(fieldName="deletedAt")
2222
*/
2323
#[ORM\Entity]
24-
#[Gedmo\SoftDeleteable(fieldName: 'deletedAt')]
24+
#[Gedmo\SoftDeleteable(fieldName: 'deletedAt', setterMethod: 'setDeletedAt')]
2525
class Comment
2626
{
2727
/**

tests/Gedmo/SoftDeleteable/SoftDeleteableEntityTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,17 @@ public function testSoftDeleteableWithDateTimeInterface(): void
432432
static::assertInstanceOf('DateTimeInterface', $foundArt->getDeletedAt());
433433
static::assertIsObject($foundComment);
434434
static::assertInstanceOf(self::OTHER_COMMENT_CLASS, $foundComment);
435+
436+
$commentField = 'comment';
437+
$commentValue = 'Comment 1';
438+
$commentRepo = $this->em->getRepository(self::COMMENT_CLASS);
439+
$comment = $commentRepo->findOneBy([$commentField => $commentValue]);
440+
441+
$this->em->remove($comment);
442+
$this->em->flush();
443+
444+
static::assertIsObject($comment->getDeletedAt());
445+
static::assertInstanceOf('DateTimeInterface', $comment->getDeletedAt());
435446
}
436447

437448
/**

tests/Gedmo/Timestampable/Fixture/Comment.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ class Comment implements Timestampable
6565
#[Gedmo\Timestampable(on: 'change', field: 'status', value: 1)]
6666
private $closed;
6767

68+
/**
69+
* @var \DateTime|null
70+
*
71+
* @ORM\Column(name="modified", type="time")
72+
*
73+
* @Gedmo\Timestampable(on="update")
74+
*/
75+
#[ORM\Column(name: 'created', type: Types::TIME_MUTABLE)]
76+
#[Gedmo\Timestampable(on: 'create', setterMethod: 'setCreated')]
77+
private $created;
78+
6879
/**
6980
* @var \DateTime|null
7081
*
@@ -109,6 +120,16 @@ public function getMessage(): ?string
109120
return $this->message;
110121
}
111122

123+
public function getCreated(): ?\DateTime
124+
{
125+
return $this->created;
126+
}
127+
128+
public function setCreated(?\DateTime $created): void
129+
{
130+
$this->created = $created;
131+
}
132+
112133
public function getModified(): ?\DateTime
113134
{
114135
return $this->modified;

0 commit comments

Comments
 (0)