Note that there are some explanatory texts on larger screens.

plurals
  1. POOverride Model::save() or Implement Before/After Events? How Do I Fatten my Cake Models?
    text
    copied!<p>I find my edit actions in CakePHP controllers get messy pretty quickly, and I'd like to pull most of that crap into the Model. Let me give you a scenario.</p> <p>I have an users/edit action in my Users controller. I want to let users reset a password (or not reset the password) in my form. If they provide a new password then I pass the three password fields into save() using the fields list parameter of save(). If they don't provide those fields I don't want to pass those fields in using the fields list. </p> <p>The code to check these fields is currently in my controller, what would be a good way to move this into the model?</p> <p>Here's what my controller's edit action looks like:</p> <pre><code>function edit($id = null) { if ($this-&gt;Session-&gt;check('Auth.User') &amp;&amp; $this-&gt;Session-&gt;read('Auth.User.id') == $id) { if (!$id &amp;&amp; empty($this-&gt;data)) { $this-&gt;Session-&gt;setFlash('Invalid Account','default',array('class'=&gt;'flash_error')); $this-&gt;redirect(array('controller'=&gt;'directories', 'action' =&gt; 'index')); } if (!empty($this-&gt;data)) { // take out the following and an error occurs in parentNode() $this-&gt;data['User']['group_id'] = 2; if (empty($this-&gt;data['User']['old_password'])) { //TODO: pass in a field list for every publicly available save() call. //dont update the password fields if they aren't passing in the old password if ($this-&gt;User-&gt;save($this-&gt;data,true,array('first_name', 'last_name', 'email', 'username'))) { $this-&gt;Session-&gt;setFlash('Your changes have been saved','default',array('class'=&gt;'flash_ok')); $this-&gt;redirect(array('controller'=&gt;'directories','action'=&gt;'index')); } else { $this-&gt;Session-&gt;setFlash('Your changes could not be saved. Please, try again.','default',array('class'=&gt;'flash_error')); } } else { //update the passwords if ($this-&gt;User-&gt;save($this-&gt;data,true,array('first_name', 'last_name', 'email', 'username', 'password', 'password_confirm', 'old_password'))) { $this-&gt;Session-&gt;setFlash('Your changes have been saved','default',array('class'=&gt;'flash_ok')); $this-&gt;redirect(array('controller'=&gt;'directories','action'=&gt;'index')); } else { $this-&gt;Session-&gt;setFlash('Your changes could not be saved. Please, try again.','default',array('class'=&gt;'flash_error')); } } } if (empty($this-&gt;data)) { $this-&gt;data = $this-&gt;User-&gt;read(array( 'first_name', 'last_name', 'email', 'username' ), $id); } $this-&gt;set('user_id',$id); $this-&gt;set('current_subscription', $this-&gt;User-&gt;Subscription-&gt;currentSubscription($id)); } else { //redirect to not authorized $this-&gt;Session-&gt;setFlash('Invalid Account','default',array('class'=&gt;'flash_error')); $this-&gt;redirect(array('controller'=&gt;'directories', 'action' =&gt; 'index')); } } </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