Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2 and Doctrine2 - history of changes of record
    text
    copied!<p>I need to keep in database history of changes of Profile. Except of profile data I must save timestamp and user_id. Query to create new history record is simple:</p> <pre><code>INSERT INTO history_profile SELECT NULL, CURRENT_TIMESTAMP(), p.* FROM profile p WHERE p.profile_id=? </code></pre> <p>Where put this query? I can't use triggers. I have several ideas:</p> <ol> <li><p>in controller in action called on submit form</p> <pre><code>$em = $this-&gt;getEntityManager(); $conn = $em-&gt;getConnection(); if (!is_null($profile-&gt;getProfileId())) { $conn-&gt;executeQuery('INSERT INTO history_profile SELECT NULL, CURRENT_TIMESTAMP(), p.* FROM profile p WHERE p.profile_id=?', array($profile-&gt;getProfileId())); } $em-&gt;persist($profile); $em-&gt;flush(); </code></pre> <p>but this query is executed each submit even if there is no updates to profile.</p></li> <li><p>in entity, as event listener to preUpdate</p> <pre><code>class Profile { /** * @ORM\preUpdate * @ORM\prePersist */ public function onPrePersist() { } } </code></pre> <p>but I don't know how to get doctrine's connection object</p></li> </ol> <p><strong>edit - EntityAudit trial</strong></p> <p>This bundle looks promising but I can't configure it. I installed it properly and when I add to config.yml:</p> <pre><code>simple_things_entity_audit: audited_entities: - AldenXyzBundle\Entity\Profile </code></pre> <p>and looked at queries after</p> <pre><code>php ./app/console doctrine:schema:update --dump-sql </code></pre> <p>but there was only SQL for create table revisions:</p> <pre><code>CREATE TABLE revisions (id INT AUTO_INCREMENT NOT NULL, timestamp DATETIME NOT NULL, username VARCHAR(255) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload