L
L
LobsterJoe2018-11-18 14:56:07
Doctrine ORM
LobsterJoe, 2018-11-18 14:56:07

How to check if a field has changed in an entity?

The situation is as follows: there is a certain entity, for example, Article, with fields &created, &updated. &updated is updated whenever I do a flush() (using PrePersist(), PreFlush()).
I want to add another field to the entity, for example, $modified, which would store the date the content was modified. Conventionally, when saving the entity, I would like to check if the content of the article ($content) has changed, and if so, write the date to $modified.
Tell me where to dig in search of the right solution from an architectural point of view.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
bears, 2018-11-18
@LobsterJoe

The preUpdate event will just work if at least one field is changed in essence, no matter which one. But if you want to know if a specific field has changed, then in the same preUpdate event, you can do this:

$unitOfWork = $entityManager->getUnitOfWork();
$unitOfWork->computeChangeSets();

$changesets = $unitOfWork->getEntityChangeSet($article);

$changesets is an associative array where the key is the name of the field and the value is an array with two elements:
0 - before, 1 - after the change.
In other words, the existence of the $changesets['content'] key indicates that this field has been changed.

A
Alexander Kuznetsov, 2018-11-18
@DarkRaven

In general, the above has been answered. From myself I want to add my piece of code that I use:

namespace AppBundle\Entity;

use DateTime;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\HasLifecycleCallbacks
 */
abstract class EntityBase
{

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

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

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

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

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

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

    public function setId($id)
    {
        $this->id = $id;
    }

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

    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'));
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question