vim/entity-before-save

There is no license information available for the latest version (1.0.0) of this package.

EntityBeforeSave module for zend framework 3

1.0.0 2018-01-20 20:10 UTC

README

Module for Zend Framework 3.

Installation

Installation of this module uses composer. For composer documentation, please refer to getcomposer.org.

composer require vim/entity-before-save

Register Vim\EntityBeforeSave in config/modules.config.php:

<?php
return [
    'Vim\EntityBeforeSave',
];

Example

<?php
namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table(name="article")
 * @ORM\Entity
 */
class ArticleEntity implements \Vim\EntityBeforeSave\Entity\BeforeSaveAwareInterface
{
    /**
     * @var integer
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $identity;

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

    /**
     * @var \DateTime
     * @ORM\Column(name="updated_at", type="datetime", nullable=true)
     */
    private $updatedAt;
    
    /**
     * Method is called before saving the entity
     * @param array $changedData
     */
    public function onBeforeSave(array $changedData): void
    {
        $currentDate = new \DateTime();
        $this->updatedAt = $currentDate;

        if (!$this->identity) {
            $this->createdAt = $currentDate;
        }
    }
}