I am new to CakePHP, so I'm still learning a lot of the behaviors and everything. One I'm struggling to find a solution to is the fact that I have a code in the AppController using beforeRender. The code saves the user's ID, their IP address, and the current time of the page load to the database in order to record activity.
I'm getting a rather curious result, and though I've looked around for an answer I haven't been able to find it. Upon loading a page that is controlled by another controller (in this case, the UserController), it saves the data to the database as well as an exact duplicate.
Right now I have just a handful of pages. The main index page, the login page, the register page, the user profile page, and the user settings page. Everything but the index page is controlled in the UserController, but I have a redirect from the login and register pages so their URLs are /login instead of /users/login. Interestingly, the duplicates aren't created on the login or register pages-- just the profile and settings page.
I had originally placed the code in beforeFilter, and I got 3 copies of the same data instead of 2. I've also tried it in afterFilter but I still got 2 copies.
I should mention, the three fields that I am updating are not located in the Users table in the database- they have their own table 'Activities'.
This is my beforeRender code in the 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, this is the controller action for the Profile page, one of the ones causing the 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 the view for the profile page (it's mostly 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>
$this->request->datafor your activities because it may contain POST data. So if one of your pages POSTs a form, there will be lots of data in$this->request->datathat may cause problems when saving to youractivitiestable. Just use$data['User']['id'] = $this->Auth->user('id');and so on. And save via$this->Activity->save($data);- CostarequestAction()anywhere? - Costa