Answers.Body: <p>You have to listen to the <code>onBeforeSaveEntry</code> event, then query the database for the existing entry (if one exists).</p>
  
  <pre class="lang-php prettyprint-override"><code>craft()-&gt;on('entries.onBeforeSaveEntry', function(Event $e)
  {
      if(!$e-&gt;params['isNewEntry'])
      {
          $updatedEntry = $e-&gt;params['entry'];
          $currentEntry = craft()-&gt;entries-&gt;getEntryById($updatedEntry-&gt;id);
  
          if($updatedEntry-&gt;getUrl() != $currentEntry-&gt;getUrl())
          {
              // Do your thing
          }
      }
  });
  </code></pre>
  
  <p>It might also be worth checking if the updated entry even has a URL, so you could skip that DB query if you don't need it.</p>
  
  <p>You have to be careful with this event though - there's always the possibility the transaction could fail and the updated entry won't get saved to the DB. In that case, you might want to also listen to the <code>onSaveEntry</code> event (if you're doing anything other than writing to the DB - this would get rolled back automatically if something fails) as a way of checking if everything went okay.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-02T22:23:14.087Z
Answers.LastActivityDate: 2016-02-02T22:44:49.427Z
Answers.LastEditDate: 2016-02-02T22:44:49.427Z
Answers.UserDisplayName: Ben Fleming
Body: <p>If an model/element gets changed then I can know about it by listening to an event like onBeforeSaveEntry. This will provide me with the latest version of the model/event, but doesn't give me the changes. Something like this would be great.</p>
  
  <pre><code>craft()-&gt;on('elements.onSaveElement', function (Event $event) {
      $element = $event-&gt;params['element'];
  
      // $changes = ['uri' =&gt; ['old' =&gt; 'index', 'new' =&gt; 'index2']]
      $changes = $element-&gt;getUnsavedChanges(); 
  }
  </code></pre>
  
  <p>I generate a routes.js that the client uses to generate menu and other stuff. I would like to listen to uri/slug changes so I can clear my (varnish) cache to update internal links. I'd like to prevent clearing my whole site if just one tiny thing gets changed. Any idea's how I can get the model changes or work around this issue?</p>
  
Tags: <plugin-development><plugins>
Title: Get model changes on save event
ThreadText: # Get model changes on save event
  
  (Asked by: username_0 on 2016-02-02)
  
  If an model/element gets changed then I can know about it by listening to an event like onBeforeSaveEntry. This will provide me with the latest version of the model/event, but doesn't give me the changes. Something like this would be great.
  
  ```
  craft()->on('elements.onSaveElement', function (Event $event) {
      $element = $event->params['element'];
  
      // $changes = ['uri' => ['old' => 'index', 'new' => 'index2']]
      $changes = $element->getUnsavedChanges(); 
  }
  
  ```
  
  I generate a routes.js that the client uses to generate menu and other stuff. I would like to listen to uri/slug changes so I can clear my (varnish) cache to update internal links. I'd like to prevent clearing my whole site if just one tiny thing gets changed. Any idea's how I can get the model changes or work around this issue?
  
  **Tags:** plugin-development, plugins
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-02-02. Score: 4 | Accepted answer
  
  You have to listen to the `onBeforeSaveEntry` event, then query the database for the existing entry (if one exists).
  
  ```php
  craft()->on('entries.onBeforeSaveEntry', function(Event $e)
  {
      if(!$e->params['isNewEntry'])
      {
          $updatedEntry = $e->params['entry'];
          $currentEntry = craft()->entries->getEntryById($updatedEntry->id);
  
          if($updatedEntry->getUrl() != $currentEntry->getUrl())
          {
              // Do your thing
          }
      }
  });
  
  ```
  
  It might also be worth checking if the updated entry even has a URL, so you could skip that DB query if you don't need it.
  
  You have to be careful with this event though - there's always the possibility the transaction could fail and the updated entry won't get saved to the DB. In that case, you might want to also listen to the `onSaveEntry` event (if you're doing anything other than writing to the DB - this would get rolled back automatically if something fails) as a way of checking if everything went okay.
  
  
  ---
  
  