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很多 –