Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I don't believe there is a built-in solution. I came up with some code that works, but you would definitely want to put it into its own function so that it can be used by multiple controller actions. Also, it's not perfect. For example, it would fail on a date field since CakePHP has date fields rendered as <code>select</code>s in forms.</p> <p>This is for an edit action. Original code:</p> <pre><code>public function edit($id = null) { $this-&gt;User-&gt;id = $id; if (!$this-&gt;User-&gt;exists()) { throw new NotFoundException(__('Invalid user')); } if ($this-&gt;request-&gt;is('post') || $this-&gt;request-&gt;is('put')) { if ($this-&gt;User-&gt;save($this-&gt;request-&gt;data)) { $this-&gt;Session-&gt;setFlash(__('The user has been saved')); $this-&gt;redirect(array('action' =&gt; 'index')); } else { $this-&gt;Session-&gt;setFlash(__('The user could not be saved. Please, try again.')); } } else { $this-&gt;request-&gt;data = $this-&gt;User-&gt;read(null, $id); } } </code></pre> <p>Modified code:</p> <pre><code>public function edit($id = null) { $this-&gt;User-&gt;id = $id; if (!$this-&gt;User-&gt;exists()) { throw new NotFoundException(__('Invalid user')); } if ($this-&gt;request-&gt;is('post') || $this-&gt;request-&gt;is('put')) { $originalData = unserialize(base64_decode($this-&gt;request-&gt;data['Extra']['original_data'])); $save = $this-&gt;request-&gt;data; unset($save['Extra']); foreach ($save as $model =&gt; $modelFields) { if (!array_key_exists($model, $originalData)) { continue; } foreach ($modelFields as $field =&gt; $value) { if (!array_key_exists($field, $originalData[$model])) { continue; } if ($save[$model][$field] === $originalData[$model][$field]) { unset($save[$model][$field]); } } } $this-&gt;User-&gt;set($save); if ($this-&gt;User-&gt;validates() &amp;&amp; $this-&gt;User-&gt;save($save, false)) { $this-&gt;Session-&gt;setFlash(__('The user has been saved')); $this-&gt;redirect(array('action' =&gt; 'index')); } else { $this-&gt;Session-&gt;setFlash(__('The user could not be saved. Please, try again.')); } } else { $this-&gt;request-&gt;data = $this-&gt;User-&gt;read(null, $id); $this-&gt;request-&gt;data['Extra']['original_data'] = base64_encode(serialize($this-&gt;request-&gt;data)); } } </code></pre> <p>Also, add this somewhere in the form in <code>edit.ctp</code>:</p> <pre><code>echo $this-&gt;Form-&gt;hidden('Extra.original_data'); </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