Wednesday, February 25, 2015

Custom method in custom LogEngine

Hi

Ive created a custom DatabaseLog Engine which logs to the database. I would like to save the user too, so I have this method:

public function write($type, $message,$user_id = null) {
if (empty($user_id)) {
$user_id = CakeSession::read('Auth.User.id');
}

$log['action'] = $type;
$log['user_id'] = (empty($user_id)) ? null : $user_id;
$log['description'] = $message;
$log['remote_address'] = ($_SERVER['REMOTE_ADDR'] == '::1') ? '127.0.0.1' : $_SERVER['REMOTE_ADDR'];
$log['created'] = date('Y-m-d H:i:s');

$this->Log->create();
return $this->Log->save($log);
}

This works if the user is logged in (Auth.User.id is set). But when the user is logged out, I would like to set the user_id manually. So I have put the user_id in the parameter list of this method.

But the LogEngine ignores this additional parameter. A custom method (like the convience ones) will be irgnored too:

public function user($message,$user_id = null) {
$log['action'] = 'user';
$log['user_id'] = (empty($user_id)) ? null : $user_id;
$log['description'] = $message;
$log['remote_address'] = ($_SERVER['REMOTE_ADDR'] == '::1') ? '127.0.0.1' : $_SERVER['REMOTE_ADDR'];
$log['created'] = date('Y-m-d H:i:s');

$this->Log->create();
return $this->Log->save($log);
}

How can I achieve to give the write method the user_id on the way?

Thanks,
Frank


--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: