Skip to content

Commit

Permalink
Merge pull request #287 from tienvx/add-datetime-to-static-case-entity
Browse files Browse the repository at this point in the history
Add datetime to static case entity
  • Loading branch information
tienvx committed Jun 19, 2019
2 parents c822b98 + 92feac7 commit 40e0ef0
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
86 changes: 86 additions & 0 deletions src/Entity/StaticCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace Tienvx\Bundle\MbtBundle\Entity;

use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Exception;
use Symfony\Component\Validator\Constraints as Assert;
use Tienvx\Bundle\MbtBundle\Validator\Constraints as MbtAssert;

/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class StaticCase
{
Expand All @@ -18,12 +21,32 @@ class StaticCase
*/
private $id;

/**
* @ORM\Column(type="string")
* @Assert\NotBlank
*/
private $model;

/**
* @ORM\Column(type="text")
* @Assert\NotNull
*/
private $path;

/**
* @var DateTime
*
* @ORM\Column(name="created_at", type="datetime")
*/
private $createdAt;

/**
* @var DateTime
*
* @ORM\Column(name="updated_at", type="datetime")
*/
private $updatedAt;

public function getId(): ?int
{
return $this->id;
Expand All @@ -43,4 +66,67 @@ public function setPath(Path $path)
{
$this->path = Path::serialize($path);
}

/**
* @MbtAssert\Model
*
* @return Model
*/
public function getModel(): Model
{
return new Model($this->model);
}

public function setModel(Model $model)
{
$this->model = $model->getName();
}

public function setCreatedAt(DateTime $createdAt)
{
$this->createdAt = $createdAt;
}

public function getCreatedAt(): ?DateTime
{
return $this->createdAt;
}

public function setUpdatedAt(DateTime $updatedAt)
{
$this->updatedAt = $updatedAt;
}

public function getUpdatedAt(): ?DateTime
{
return $this->updatedAt;
}

/**
* @ORM\PrePersist
*
* @throws Exception
*/
public function prePersist()
{
if (!$this->getCreatedAt()) {
$this->setCreatedAt(new DateTime());
}

if (!$this->getUpdatedAt()) {
$this->setUpdatedAt(new DateTime());
}
}

/**
* @ORM\PreUpdate
*
* @throws Exception
*/
public function preUpdate()
{
if (!$this->getUpdatedAt()) {
$this->setUpdatedAt(new DateTime());
}
}
}
1 change: 1 addition & 0 deletions tests/Message/TestStaticCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function testExecute(string $model, string $generator, string $reducer)
]);

$staticCase = new StaticCase();
$staticCase->setModel(new Model($model));
$staticCase->setPath($path);
$entityManager->persist($staticCase);

Expand Down

0 comments on commit 40e0ef0

Please sign in to comment.