Symfony perfect entity, dates and data

Dates with \DateTime

/**
*
@var \DateTime
*/
protected $createdAt;

/**
*
@var \DateTime
*/
protected $updatedAt;
/**
*
@return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}

/**
*
@param \DateTime $createdAt
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
}

/**
*
@return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}

/**
*
@param \DateTime $updatedAt
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
}

Doctrine and Lifecycle Callbacks

<?php

namespace
AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

trait TimestampableEntity
{
/**
*
@var \DateTime
*
*
@ORM\Column(type="datetime", nullable=true)
*/
protected $createdAt;

/**
*
@var \DateTime
*
*
@ORM\Column(type="datetime", nullable=true)
*/
protected $updatedAt;


/**
*
@return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}

/**
*
@param \DateTime $createdAt
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
}

/**
*
@return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}

/**
*
@param \DateTime $updatedAt
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
}

/**
*
@ORM\PrePersist
* @ORM\PreUpdate
*/
public function updatedTimestamps()
{
$this->setUpdatedAt(new \DateTime('now'));

if ($this->getCreatedAt() == null) {
$this->setCreatedAt(new \DateTime('now'));
}
}
}
<?php

namespace
AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
*
@ORM\Entity(repositoryClass="AppBundle\Repository\AppRepository")
*
@ORM\Table(name="PRODUCT")
*
@ORM\HasLifecycleCallbacks
*/
class Product
{
use TimestampableEntity;

/**
*
@var int
*
*
@ORM\Column(name="id", type="integer", nullable=false)
*
@ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
*
@var float
*
*
@ORM\Column(type="float", nullable=true)
*/
protected $price;

/**
*
@var int
*
*
@ORM\Column(type="integer", nullable=true)
*/
protected $amount;

/**
*
@return int
*/
public function getId(): int
{
return $this->id;
}

/**
*
@return float
*/
public function getPrice(): float
{
return $this->price;
}

/**
*
@param float $price
*/
public function setPrice(float $price)
{
$this->price = $price;
}

/**
*
@return int
*/
public function getAmount(): int
{
return $this->amount;
}

/**
*
@param int $amount
*/
public function setAmount(int $amount)
{
$this->amount = $amount;
}
}

Entity data

<?php

namespace
AppBundle\Traits;

use Doctrine\Common\Inflector\Inflector;

trait DataLoader
{
/**
*
@param array $data
*
*
@return $this
*/
public function loadData(array $data)
{
foreach ($data as $key => $value) {
$method = Inflector::camelize('set_' . $key);
if (method_exists($this, $method)) {
$this->$method($value);
}
}

return $this;
}
}
...
class
Product
{
use TimestampableEntity;
use DataLoader;
...
$product = new Product();
$product->loadData(
[
'price' => 12.23,
'amount' => 10,
]
);

Entity properties to Array

<?php

namespace
AppBundle\Traits;

trait RawLoader
{
/**
*
@return array
*/
public function toArray()
{
return get_object_vars($this);
}
}
...
class
Product
{
use TimestampableEntity;
use DataLoader;
use RawLoader;
...
$product->toArray();

GitHub

Other articles

Thank you for your attention!

Senior web developer, architect, cryptocurrencies trader, https://lytvynov-anton.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store