php - In CakePHP, beforeRender in the AppController is saving to database twice -


i new cakephp, i'm still learning lot of behaviors , everything. 1 i'm struggling find solution fact have code in appcontroller using beforerender. code saves user's id, ip address, , current time of page load database in order record activity.

i'm getting rather curious result, , though i've looked around answer haven't been able find it. upon loading page controlled controller (in case, usercontroller), saves data database exact duplicate.

right have handful of pages. main index page, login page, register page, user profile page, , user settings page. index page controlled in usercontroller, have redirect login , register pages urls /login instead of /users/login. interestingly, duplicates aren't created on login or register pages-- profile , settings page.

i had placed code in beforefilter, , got 3 copies of same data instead of 2. i've tried in afterfilter still got 2 copies.

i should mention, 3 fields updating not located in users table in database- have own table 'activities'.

this beforerender code in appcontroller:

public function beforerender() {     if ($user = $this->session->read('auth.user')) {         $this->loadmodel('user');         $this->loadmodel('activity');         $data['user']['id'] = $this->auth->user('id');         $data['activity']['user_ip'] = $this->request->clientip();         $data['activity']['user_id'] = $data['user']['id'];         $data['activity']['last_activity'] = date("y-m-d h:i:s");         $this->activity->save($data);     } } 

per request, controller action profile page, 1 of ones causing duplicates:

public function profile($id = null) {         $this->loadmodel('user');         $this->user->id = $id;         $this->request->data['user']['id'] = $this->auth->user('id');         if (!$this->user->exists()) {             throw new notfoundexception(__('user not found'));         }         $user = $this->user->findbyid($id);         if (!$user) {         throw new notfoundexception(__('user not found'));         }         $this->set('user', $this->user->read(null, $id));         $this->set('title_for_layout', $user['user']['screenname'] . "'s" . ' ' . 'profile');     } 

and view profile page (it's basic html):

<?php echo $this->html->css('profile');  ?>  <h2><?php echo h($user['user']['screenname']); ?></h2>  <center><table cellpadding="6"> <tr><center>     <td><center><div class="ha_image"></div>     </td>     <td><center><h4>user information</h4>     <div class="user_info">         <table class="">             <tr>                 <td class="stats_names">                     member type                 </td>                 <td class="stats_stats">                     --                 </td>             </tr>             <tr>                 <td class="stats_names">                     member since                 </td>                 <td class="stats_stats">                     <?php echo date('m js, y', strtotime($user['user']['created'])); ?>                 </td>             </tr>             <tr>                 <td class="stats_names">                     last active                 </td>                 <td class="stats_stats">                     <?php echo date('m j, y h:i a', strtotime($user['user']['last_active'])); ?>                 </td>             </tr>             <tr>                 <td class="laststats_names">                     status                 </td>                 <td class="laststats_stats">                  </td>             </tr>             <tr>                 <td class="stats_names">                     totals                 </td>                 <td class="stats_stats">                     --                 </td>             </tr>             <tr>                 <td class="stats_names">                     forum posts                 </td>                 <td class="stats_stats">                     --                 </td>             </tr>             <tr>                 <td class="stats_names">                     achievements                 </td>                 <td class="stats_stats">                     --                 </td>             </tr>             <tr>                 <td class="laststats_names">                     quests done                 </td>                 <td class="laststats_stats">                     --                 </td>             </tr>         </table> 

maybe because have

$this->redirect(array('action' => 'index')); 

in controller. when index page load, beforerender function called again.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -