模型中调用插件

在模型中调用调用插件,首先要在模型中实现SplSubject接口,然后在模型方法中通知插件处理。

§   实现SplSubject接口

使用implements操作符实现SplSubject接口,完成SplSubject接口中的三个方法。定义如下:

class model_模型名称 extends model implements SplSubject
{
            private $observers = array();
 
            public function attach(SplObserver $observer)
            {
                        $this->observers[] = $observer;
            }
 
            public function detach(SplObserver $observer)
            {
                        if($index = array_search($observer, $this->observers, true)) unset($this->observers[$index]);
            }
 
            public function notify()
            {
                        foreach ($this->observers as $observer)
                        {
                                   $observer->update($this);
                        }
            }
}

我们继续以guestbook模型为例,为guestbook实现SplSubject接口后的完整代码为:

class model_guestbook extends model  implements SplSubject
{
            function __construct()
            {
                        parent::__construct();
 
                        $this->_table = $this->db->options['prefix'].'guestbook';
                        $this->_primary = 'gid';
                        $this->_fields = array('gid','typeid', 'title', 'content', 'userid', 'username', 'gender', 'email','qq','msn','telephone','address','homepage','isview','ip','addtime','reply','replyer','replytime');
                        $this->_readonly = array('gid');
                        $this->_create_autofill = array('userid'=>$this->_userid, 'username'=>$this->_username, 'addtime'=>TIME, 'ip'=>IP);
                        $this->_update_autofill = array();
                        $this->_filters_input = array();
                        $this->_filters_output = array();
                        $this->_validators = array(
                                   'title'=>array(
                                               'not_empty'=>array('
留言标题不能为空'),
                                               'max_length'=>array(100, '
留言标题不能超过100个字符'),
                                   )
                        );
            }
 
 
            public function attach(SplObserver $observer)
            {
                        $this->observers[] = $observer;
            }
 
            public function detach(SplObserver $observer)
            {
                        if($index = array_search($observer, $this->observers, true)) unset($this->observers[$index]);
            }
 
            public function notify()
            {
                        foreach ($this->observers as $observer)
                        {
                                   $observer->update($this);
                        }
            }
}

§   在方法中通知插件

function method()
{
            /*
插件之前的处理流程  */
            $this->event = '
插件方法';       //指定要通知处理的插件方法名
            $this->notify();                        //
发送通知,执行插件中相应的方法
            /*
插件之后的处理流程 */
}

举例说明

继续以guestook模型为例,我们希望在添加留言成功之后,给管理员发送一封邮件。这个发送邮件的动作就可以定义为插件来执行。具体代码如下:

§   模型的代码,实现SplSubject接口,并在add方法中执行插件通知 after_insert 方法

class model_guestbook extends model  implements SplSubject
{
            private $observers = array();
            public $data;
 
            function __construct()
            {
                        parent::__construct();
 
                        $this->_table = $this->db->options['prefix'].'guestbook';
                        $this->_primary = 'gid';
                        $this->_fields = array('gid','typeid', 'title', 'content', 'userid', 'username', 'gender', 'email','qq','msn','telephone','address','homepage','isview','ip','addtime','reply','replyer','replytime');
                        $this->_readonly = array('gid');
                        $this->_create_autofill = array('userid'=>$this->_userid, 'username'=>$this->_username, 'addtime'=>TIME, 'ip'=>IP);
                        $this->_update_autofill = array();
                        $this->_filters_input = array();
                        $this->_filters_output = array();
                        $this->_validators = array(
                                   'title'=>array(
                                               'not_empty'=>array('
留言标题不能为空'),
                                               'max_length'=>array(100, '
留言标题不能超过100个字符'),
                                   )
                        );
            }
 
            public function add($data)
            {
                        $this->data = $data;
                        if($result = $this->insert($this->data))
                        {
                                   //
执行插件
                                   $this->event = 'after_insert';
                                   $this->notify();
                                   return $result;
                        }
                        else
                        {
                                   return FALSE;
                        }
            }
 
            public function attach(SplObserver $observer)
            {
                        $this->observers[] = $observer;
            }
 
            public function detach(SplObserver $observer)
            {
                        if($index = array_search($observer, $this->observers, true)) unset($this->observers[$index]);
            }
 
            public function notify()
            {
                        foreach ($this->observers as $observer)
                        {
                                   $observer->update($this);
                        }
            }
}

§   插件配置文件

配置文件

./apps/guestbook/plugin/model_guestbook/config.php

配置内容

return array(
            'mail'=>array('after_insert'),
);

§   插件定义及方法实现

插件文件

./apps/guestbook/plugin/model_guestbook/mail.php

插件内容

class plugin_mail extends object
{
            private $model;
 
            public function __construct(& $model)
            {
                        $this->model= $model;
            }
 
            public function after_insert()
            {
                        $data = $this->model->data;
                        $mail = & factory::sendmail();
                        $mail->execute($data['email'], '
网站有新的留言','网站有新的留言了,快来回复他吧。');
            }
}



Copyright ©2009 - 2014 CmsTop.Com.All rights reserved.
思拓合众