Skip to content

Commit dc6063e

Browse files
committed
Add support for embeddable, add test
1 parent d497a17 commit dc6063e

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

lib/Gedmo/AbstractTrackingListener.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,19 @@ public function onFlush(EventArgs $args)
106106
$trackedFields = $options['trackedField'];
107107
}
108108

109-
foreach ($trackedFields as $tracked) {
109+
foreach ($trackedFields as $trackedField) {
110110
$trackedChild = null;
111-
$parts = explode('.', $tracked);
111+
$parts = explode('.', $trackedField);
112112
if (isset($parts[1])) {
113113
$tracked = $parts[0];
114114
$trackedChild = $parts[1];
115115
}
116116

117+
if (!isset($tracked) || array_key_exists($trackedField, $changeSet)) {
118+
$tracked = $trackedField;
119+
unset($trackedChild);
120+
}
121+
117122
if (isset($changeSet[$tracked])) {
118123
$changes = $changeSet[$tracked];
119124
if (isset($trackedChild)) {

tests/Gedmo/Timestampable/Fixture/Article.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ class Article implements Timestampable
2828
*/
2929
private $comments;
3030

31+
/**
32+
* @ORM\Embedded(class="Timestampable\Fixture\Author")
33+
*/
34+
private $author;
35+
3136
/**
3237
* @var datetime $created
3338
*
@@ -59,6 +64,13 @@ class Article implements Timestampable
5964
* @Gedmo\Timestampable(on="change", field={"title", "body"})
6065
*/
6166
private $contentChanged;
67+
/**
68+
* @var datetime $authorChanged
69+
*
70+
* @ORM\Column(name="author_changed", type="datetime", nullable=true)
71+
* @Gedmo\Timestampable(on="change", field={"author.name", "author.email"})
72+
*/
73+
private $authorChanged;
6274

6375
/**
6476
* @ORM\ManyToOne(targetEntity="Type", inversedBy="articles")
@@ -106,6 +118,16 @@ public function getComments()
106118
return $this->comments;
107119
}
108120

121+
public function getAuthor()
122+
{
123+
return $this->author;
124+
}
125+
126+
public function setAuthor(Author $author)
127+
{
128+
$this->author = $author;
129+
}
130+
109131
/**
110132
* Get created
111133
*
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
namespace Timestampable\Fixture;
3+
4+
use Gedmo\Timestampable\Timestampable;
5+
use Gedmo\Mapping\Annotation as Gedmo;
6+
use Doctrine\ORM\Mapping as ORM;
7+
8+
/**
9+
* @ORM\Embeddable()
10+
*/
11+
class Author
12+
{
13+
/**
14+
* @ORM\Column(name="author_name", type="string", length=128)
15+
*/
16+
private $name;
17+
18+
/**
19+
* @ORM\Column(name="author_email", type="string", length=50)
20+
*/
21+
private $email;
22+
23+
public function getName()
24+
{
25+
return $this->name;
26+
}
27+
28+
public function setName($name)
29+
{
30+
$this->name = $name;
31+
}
32+
33+
public function getEmail()
34+
{
35+
return $this->email;
36+
}
37+
38+
public function setEmail($email)
39+
{
40+
$this->email = $email;
41+
}
42+
43+
44+
}

tests/Gedmo/Timestampable/TimestampableTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Gedmo\Timestampable;
44

55
use Doctrine\Common\EventManager;
6+
use Timestampable\Fixture\Author;
67
use Tool\BaseTestCaseORM;
78
use Timestampable\Fixture\Article;
89
use Timestampable\Fixture\Comment;
@@ -93,6 +94,12 @@ function shouldHandleStandardBehavior()
9394
$sportComment->setArticle($sport);
9495
$sportComment->setStatus(0);
9596

97+
$author = new Author();
98+
$author->setName('Original author');
99+
$author->setEmail('[email protected]');
100+
101+
$sport->setAuthor($author);
102+
96103
$this->em->persist($sport);
97104
$this->em->persist($sportComment);
98105
$this->em->flush();
@@ -102,6 +109,11 @@ function shouldHandleStandardBehavior()
102109
$this->assertNotNull($su = $sport->getUpdated());
103110
$this->assertNull($sport->getContentChanged());
104111
$this->assertNull($sport->getPublished());
112+
$this->assertNull($sport->getAuthorChanged());
113+
114+
$author = $sport->getAuthor();
115+
$author->setName('New author');
116+
$sport->setAuthor($author);
105117

106118
$sportComment = $this->em->getRepository(self::COMMENT)->findOneByMessage('hello');
107119
$this->assertNotNull($scm = $sportComment->getModified());
@@ -120,6 +132,7 @@ function shouldHandleStandardBehavior()
120132
$sportComment = $this->em->getRepository(self::COMMENT)->findOneByMessage('hello');
121133
$this->assertNotNull($scc = $sportComment->getClosed());
122134
$this->assertNotNull($sp = $sport->getPublished());
135+
$this->assertNotNull($sa = $sport->getAuthorChanged());
123136

124137
$sport->setTitle('Updated');
125138
$this->em->persist($sport);
@@ -131,6 +144,11 @@ function shouldHandleStandardBehavior()
131144
$this->assertNotSame($su2 = $sport->getUpdated(), $su, "Date updated should change after update");
132145
$this->assertSame($sport->getPublished(), $sp, "Date published should remain the same after update");
133146
$this->assertNotSame($scc2 = $sport->getContentChanged(), $scc, "Content must have changed after update");
147+
$this->assertSame($sport->authorChanged(), $sa, "Author should remain same after update");
148+
149+
$author = $sport->getAuthor();
150+
$author->setName('Third author');
151+
$sport->setAuthor($author);
134152

135153
$sport->setBody('Body updated');
136154
$this->em->persist($sport);
@@ -142,6 +160,7 @@ function shouldHandleStandardBehavior()
142160
$this->assertNotSame($sport->getUpdated(), $su2, "Date updated should change after update");
143161
$this->assertSame($sport->getPublished(), $sp, "Date published should remain the same after update");
144162
$this->assertNotSame($sport->getContentChanged(), $scc2, "Content must have changed after update");
163+
$this->assertNotSame($sport->getAuthorChanged(), $sa, "Author must have changed after update");
145164
}
146165

147166
/**

0 commit comments

Comments
 (0)