1
我在cakephp中使用事件监听器来保存通知之后喜欢的帖子 一切正常,我的事件监听器功能很好 但我想我无法访问我的通知表事件函数 这是我的错误:在cakephp2.x事件函数中保存数据在数据库中
Undefined property: AddNotification::$Notification [APP\Lib\Event\AddNotification.php, line 15]
这是我保存投票模型喜欢:
App::uses('CakeEvent', 'Event');
class Vote extends AppModel {
var $useTable = 'votes';
public $actsAs = array('Containable');
public $belongsTo = array('User','Tail');
public function afterSave($created, $options = array()) {
if ($created) {
$event = new CakeEvent('Model.Vote.created', $this, array(
'data' => $this->data[$this->alias]
));
$this->getEventManager()->dispatch($event);
}
}
和我AddNotification.php功能:
<?php
App::uses('CakeEventListener', 'Event');
class AddNotification implements CakeEventListener {
public function implementedEvents() {
return array(
'Model.Vote.created' => 'addANotification',
);
}
public function addANotification($event) {
// Code to update votes
pr($event->data['data']);
$notified = $this->Notification->find('all',array(
'conditions'=>array('Notification.user_id'=>$this->CustomAuth->User('id'),'Notification.tail_id'=>$this->data['tail_id'])
));
// pr($notified);
if(!empty($notified)) {
$this->Notification->delete($notified[0]['Notification']['id']);
$this->Notification->create();
$notifi['Notification']['user_id'] = $this->CustomAuth->user('id');
$notifi['Notification']['tail_id'] = $event->data['data']['tail_id'];
if($event->data['data']['value']==1) {
$notifi['Notification']['message'] = "Tail has been liked";
}
else {
$notifi['Notification']['message'] = "Tail has been disliked";
}
if ($this->Notification->save($notifi)) {
$message = 'notifi saved';
}
else {
$message = 'notifi not saved';
}
}
else {
$this->Notification->create();
$notifi['Notification']['user_id'] = $this->CustomAuth->user('id');
$notifi['Notification']['tail_id'] = $event->data['data']['tail_id'];
if($event->data['data']['value']==1) {
$notifi['Notification']['message'] = "Tail has been liked";
}
else {
$notifi['Notification']['message'] = "Tail has been disliked";
}
if ($this->Notification->save($notifi)) {
$message = 'notifi saved';
}
else {
$message = 'notifi not saved';
}
}
}
}
?>
请帮我改善它
It works tnx很多 –