Skip to content

Commit 4968c59

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 9ffa94d commit 4968c59

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
@@ -16,6 +16,7 @@
1616
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs;
1717
use Doctrine\Persistence\Mapping\ClassMetadata;
1818
use Doctrine\Persistence\ObjectManager;
19+
use Gedmo\Exception\InvalidMappingException;
1920
use Gedmo\Mapping\MappedEventSubscriber;
2021

2122
/**
@@ -86,7 +87,18 @@ public function onFlush(EventArgs $args)
8687
$ea->createLifecycleEventArgsInstance($object, $om)
8788
);
8889

89-
$reflProp->setValue($object, $date);
90+
if (!empty($config['setterMethod'][$config['fieldName']])) {
91+
$reflectionClass = $meta->getReflectionClass();
92+
$setterName = $config['setterMethod'][$config['fieldName']];
93+
94+
if (!$reflectionClass->hasMethod($setterName)) {
95+
throw new InvalidMappingException("Setter method - [{$setterName}] does not exist in class - {$meta->getName()}");
96+
}
97+
98+
$reflectionClass->getMethod($setterName)->invoke($object, $date);
99+
} else {
100+
$reflProp->setValue($object, $date);
101+
}
90102

91103
$om->persist($object);
92104
$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
@@ -429,6 +429,17 @@ public function testSoftDeleteableWithDateTimeInterface(): void
429429
static::assertInstanceOf('DateTimeInterface', $foundArt->getDeletedAt());
430430
static::assertIsObject($foundComment);
431431
static::assertInstanceOf(self::OTHER_COMMENT_CLASS, $foundComment);
432+
433+
$commentField = 'comment';
434+
$commentValue = 'Comment 1';
435+
$commentRepo = $this->em->getRepository(self::COMMENT_CLASS);
436+
$comment = $commentRepo->findOneBy([$commentField => $commentValue]);
437+
438+
$this->em->remove($comment);
439+
$this->em->flush();
440+
441+
static::assertIsObject($comment->getDeletedAt());
442+
static::assertInstanceOf('DateTimeInterface', $comment->getDeletedAt());
432443
}
433444

434445
/**

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)