繁体   English   中英

在cakephp2.x事件函数中将数据保存到数据库中

[英]saving data in database in cakephp2.x event function

我在cakephp中使用事件监听器保存通知后,就像发贴一样,一切正常,我的事件监听器功能运行良好,但我认为我无法从事件函数访问通知表,这是我的错误:

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';

            }
        }
    }
}

 ?>

请帮助我改善它

未定义的属性:AddNotification :: $ Notification [APP \\ Lib \\ Event \\ AddNotification.php,第15行]

错误很明显吗? 是什么使您认为可以在某个地方神奇地访问模型实例的不存在的属性?

您需要先加载模型实例。

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

您将在此行得到相同的错误

$this->CustomAuth

看来您不了解在这种情况下如何传递和实例化对象。 代码看起来就像您只是将控制器代码复制并粘贴到侦听器类中一样。 这行不通。 花一些时间尝试理解MVC架构以及事件如何触发而不是触发它们。 向您解释我在这里看到的所有含义只是很长时间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM