Skip to content

Commit 2ef790d

Browse files
vid-1
1 parent 695d01a commit 2ef790d

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

src/AppBundle/Entity/Traits/TimestampableTrait.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ public function setCreatedAt(\DateTimeImmutable $dateTime = null)
5858
return $this;
5959
}
6060

61+
/**
62+
* @ORM\PrePersist()
63+
* @return $this
64+
*/
65+
public function setCreatedAtViaPrePersist()
66+
{
67+
return $this->setCreatedAt();
68+
}
69+
6170
/**
6271
* Get updated at
6372
*

src/AppBundle/Entity/Widget.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22

33
namespace AppBundle\Entity;
44

5+
use AppBundle\Entity\Traits\TimestampableTrait;
56
use Doctrine\ORM\Mapping as ORM;
67
use JMS\Serializer\Annotation as JMSSerializer;
78

89
/**
910
* @ORM\Entity()
1011
* @ORM\Table(name="widget")
12+
* @ORM\HasLifecycleCallbacks()
1113
* @JMSSerializer\ExclusionPolicy("all")
1214
*/
1315
class Widget
1416
{
17+
use TimestampableTrait;
18+
1519
/**
1620
* @ORM\Id
1721
* @ORM\Column(type="integer")

src/AppBundle/Features/Context/WidgetSetupContext.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,29 @@ public function thereAreWidgetsWithTheFollowingDetails(TableNode $widgets)
4040

4141
$this->em->persist($widget);
4242
$this->em->flush();
43+
44+
45+
$qb = $this->em->createQueryBuilder();
46+
47+
$query = $qb->update('AppBundle:Widget', 'w')
48+
->set(
49+
'w.createdAt',
50+
$qb->expr()->literal(
51+
(new \DateTimeImmutable($val['created_at']))->format('c')
52+
)
53+
)
54+
->set(
55+
'w.updatedAt',
56+
$qb->expr()->literal(
57+
(new \DateTimeImmutable($val['updated_at']))->format('c')
58+
)
59+
)
60+
->where('w.id = :id')
61+
->setParameter('id', $widget->getId())
62+
->getQuery()
63+
;
64+
65+
$query->execute();
4366
}
4467
}
4568
}

src/AppBundle/Features/widget.feature

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ Feature: Manage Widget data via a JSON API
77

88
Background:
99
Given there are Widgets with the following details:
10-
| id | name |
11-
| 1 | Widget A |
12-
| 2 | Widget B |
13-
| 3 | Widget C |
10+
| id | name | created_at | updated_at |
11+
| 1 | Widget A | -4 days | -5 minutes |
12+
| 2 | Widget B | -1 day | -1 day |
13+
| 3 | Widget C | -6 months | -3 weeks |
1414
And I set header "Content-Type" with value "application/json"
1515

1616

@@ -21,6 +21,22 @@ Feature: Manage Widget data via a JSON API
2121
"""
2222
{
2323
"id": 1,
24-
"name": "Widget A"
24+
"name": "Widget A",
25+
features: [
26+
{
27+
"id": 1,
28+
"name": "some feature",
29+
"created_at": "2015-01-01T00:00:00+0000",
30+
"updated_at": "2015-01-01T00:00:00+0000",
31+
},
32+
{
33+
"id": 1,
34+
"name": "another feature",
35+
"created_at": "2016-01-01T00:00:00+0000",
36+
"updated_at": "2016-01-01T00:00:00+0000",
37+
}
38+
]
2539
}
2640
"""
41+
And the "created_at" date should be approximately "-4 days"
42+
And the "updated_at" date should be approximately "-5 minutes"

0 commit comments

Comments
 (0)