2016-02-29 52 views
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'; 

      } 
     } 
    } 
} 

?> 

请帮我改善它

回答

1

Undefined property: AddNotification::$Notification [APP\Lib\Event\AddNotification.php, line 15]

的错误是很清楚吗?是什么让你认为你可以神奇地访问某个模型实例的非现有属性?

您需要先加载模型实例。

$notification = ClassRegistry::init('Notification'); 
$notification->find(/*...*/); 

你会得到同样的错误此行

$this->CustomAuth 

它看起来就像你有没有怎么样的对象是各地通过,在这种情况下实例化的理解。代码看起来像你只是将控制器代码复制并粘贴到监听器类中。这不起作用。花一些时间尝试理解MVC体系结构以及事件如何在触发它们之外进行工作。向你解释我在这里看到的所有影响只是漫长的。

+0

It works tnx很多 –