Created
January 11, 2023 13:07
-
-
Save radynsade/44ea45319491ff23f1e9040df5fc4eae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Core\Models; | |
use DateTime; | |
use Phalcon\Mvc\Model\Behavior\Timestampable; | |
use Utils\Time\Time; | |
abstract class TraceableModel extends BaseModel { | |
/** | |
* @var int|null | |
*/ | |
protected ?string $createdAt = null; | |
/** | |
* @var string|null | |
*/ | |
protected ?string $updatedAt = null; | |
/** | |
* @return DateTime|null | |
*/ | |
public function getCreatedAt(): ?DateTime { | |
if (empty($this->createdAt)) { | |
return null; | |
} | |
return new DateTime($this->createdAt); | |
} | |
/** | |
* @param int $createdAt | |
* @return static | |
*/ | |
public function setCreatedAt(string $createdAt): static { | |
$this->createdAt = $createdAt; | |
return $this; | |
} | |
/** | |
* @return DateTime|null | |
*/ | |
public function getUpdatedAt(): ?DateTime { | |
if (empty($this->updatedAt)) { | |
return null; | |
} | |
return new DateTime($this->updatedAt); | |
} | |
/** | |
* @param int $updatedAt | |
* @return static | |
*/ | |
public function setUpdatedAt(string $updatedAt): static { | |
// $this->updatedAt = $updatedAt->format(Time::DATETIME_FORMAT); | |
$this->updatedAt = $updatedAt; | |
return $this; | |
} | |
/** | |
* @return void | |
*/ | |
public function initialize(): void { | |
parent::initialize(); | |
$this->addBehavior(new Timestampable([ | |
'beforeValidationOnCreate' => [ | |
'field' => 'created_at', | |
'format' => Time::DATETIME_FORMAT, | |
], | |
])); | |
$this->addBehavior(new Timestampable([ | |
'beforeValidationOnUpdate' => [ | |
'field' => 'updated_at', | |
'format' => Time::DATETIME_FORMAT, | |
], | |
'beforeValidationOnCreate' => [ | |
'field' => 'updated_at', | |
'format' => Time::DATETIME_FORMAT, | |
], | |
])); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment