Yii私人消息扩展"方法用户:: getFullName未定义"

时间:2015-02-10 02:55:32

标签: php yii yii-extensions

我在配置私人消息扩展时遇到问题。 以下是 protected / config / main.php

的配置
'message'=>array(
'userModel'=>'User',
            'getNameMethod' => 'getFullName',
            'getSuggestMethod' => 'getSuggest', 
            'receiverRelation'=> array(
                CActiveRecord::BELONGS_TO, 'User', 'on'=>'User.id = receiver_id'
            ),
            'senderRelation'=> array(
                CActiveRecord::BELONGS_TO, 'User', 'on'=>'User.id = sender.id'
            ),
        ),

这是 protected / modules / message / MessageModule.php

的代码
<?php

class MessageModule extends CWebModule
{
    public $defaultController = 'inbox';

    public $userModel = 'User';
    public $userModelRelation = null;
    public $getNameMethod;
    public $getSuggestMethod;

    public $senderRelation;
    public $receiverRelation;

    public $dateFormat = 'Y-m-d H:i:s';

    public $inboxUrl = array("/message/inbox");

    public $viewPath = '/message/default';

    public function init()
    {
        if (!class_exists($this->userModel)) {
            throw new Exception(MessageModule::t("Class {userModel} not defined", array('{userModel}' => $this->userModel)));
        }

        foreach (array('getNameMethod', 'getSuggestMethod') as $methodName) {
            if (!$this->$methodName) {
                throw new Exception(MessageModule::t("Property MessageModule::{methodName} not defined", array('{methodName}' => $methodName)));
            }

            if (!method_exists($this->userModel, $this->$methodName)) {
                throw new Exception(MessageModule::t("Method {userModel}::{methodName} not defined", array('{userModel}' => $this->userModel, '{methodName}' => $this->$methodName)));
            }
        }

        // this method is called when the module is being created
        // you may place code here to customize the module or the application

        // import the module-level models and components
        $this->setImport(array(
            'message.models.*',
            'message.components.*',
        ));
    }

    public function beforeControllerAction($controller, $action)
    {
        if (Yii::app()->user->isGuest) {
            if (Yii::app()->user->loginUrl) {
                $controller->redirect($controller->createUrl(reset(Yii::app()->user->loginUrl)));
            } else {
                $controller->redirect($controller->createUrl('/'));
            }
        } else if (parent::beforeControllerAction($controller, $action)) {
            // this method is called before any module controller action is performed
            // you may place customized code here
            return true;
        } else {
            return false;
        }
    }

    public static function t($str='',$params=array(),$dic='message') {
        return Yii::t("MessageModule.".$dic, $str, $params);
    }

    public function getCountUnreadedMessages($userId) {
        return Message::model()->getCountUnreaded($userId);
    }

}

然而,当我尝试运行它时,我发现“Method User :: getFullName not defined”

上的问题

请帮忙...... 感谢...

1 个答案:

答案 0 :(得分:0)

哇......问题解决了 谢谢PeterM ...... 我在用户模型上添加了方法getFullName()和getSuggest($ q)..

public function getFullName()
{
    return $this->username;
}

public function getSuggest($q)
{
    $c = new CDbCriteria();
    $c->addSearchCondition('username', $q, true);
    return $this->findAll($c);
}

我误将其添加到用户控制器中 谢谢你的帮助... ^ _ ^