Symfony2 and Doctrine2 - record change history

I need to save profile changes in the database history. With the exception of profile data, I have to save timestamp and user_id. The request to create a new history record is simple:

INSERT INTO history_profile 
SELECT NULL, CURRENT_TIMESTAMP(), p.* FROM profile p WHERE p.profile_id=?

Where is this request? I can not use triggers. I have some ideas:

  • in the controller in the action called in the form of sending

    $em = $this->getEntityManager();
    $conn = $em->getConnection();
    if (!is_null($profile->getProfileId()))
    {
        $conn->executeQuery('INSERT INTO history_profile SELECT NULL, CURRENT_TIMESTAMP(), p.* FROM profile p WHERE p.profile_id=?', array($profile->getProfileId()));
    }
    $em->persist($profile);
    $em->flush();
    

    but this request is executed each, even if updates are not updated.

  • essentially as an event listener for preUpdate

    class Profile {
        /**
         * @ORM\preUpdate
         * @ORM\prePersist
         */
        public function onPrePersist()
        {
        }
    }
    

    but I do not know how to get the doctrine compound object

edit - trial version of EntityAudit

This package looks promising, but I cannot configure it. I installed it correctly and add it to config.yml:

simple_things_entity_audit:
    audited_entities:
        - AldenXyzBundle\Entity\Profile

and looked at requests after

php ./app/console doctrine:schema:update --dump-sql

but there was only SQL to create table editions:

CREATE TABLE revisions (id INT AUTO_INCREMENT NOT NULL, 
timestamp DATETIME NOT NULL, username VARCHAR(255) NOT NULL, 
PRIMARY KEY(id)) ENGINE = InnoDB
+3
source
3

, . POST EntityManager , ( ). .

0
+7

Doctrine2 . datetime . , , createdOn / modifiedOn . , sql . sql, Custom Entity.

I'm not sure if you are looking only for the timestamp "createdOn" or "updatedOn" or both. Doctrine versioning (section 9.2.1) could be thought of and used as an updated.nn date and time field.

+1
source

All Articles