$model->;save()给我一个错误


$model->save() giving me an error

我正在尝试创建一个注释系统。当我的表单发布时,并调用我的"actionCreate"函数。我在firebug上出错了。但是数据存储在我的数据库中。知道我做错了什么吗?

这是我在控制器中的操作

    public function actionCreate()
    {
         /** @var Comment $comment */
         $comment = Yii::createComponent($this->module->commentModelClass);
        // Uncomment the following line if AJAX validation is needed
         $user=new User;
         $this->performAjaxValidation(array($comment, $user));
         //end of Ajax validation
        $_user = Yii::app()->request->getPost('User');
        if(isset($_POST[$cClass=get_class($comment)], $_user))
        {
            $comment->attributes = $_POST[$cClass];
            //$comment->type = $_POST[$cClass]['type'];
            //$comment->key  = $_POST[$cClass]['key'];
            $user->attributes = $_user;
            // determine current users id
            if (Yii::app()->user->isGuest) {
                $comment->userId = null;
            } else {
                $comment->userId = Yii::app()->user->id;
            }
            if(Yii::app()->request->isAjaxRequest) 
            {
                /* the error happens here */
                if($comment->save())
                {
                    // refresh model to replace CDbExpression for timestamp attribute
                    $comment->refresh();
                    // create new comment model for empty form
                    $comment = Yii::createComponent($this->module->commentModelClass);
                    //$comment->type = $_POST[$cClass]['type'];
                    //$comment->key  = $_POST[$cClass]['key'];
                }
                else { 
                     //Return messages from try/catch
                    echo CJSON::encode(array(
                        'msg' => 'Check your form, and fill up all required fields'
                    ));
                }
                Yii::app()->end();
            } 
            else 
            {
                if($comment->save()) {
                    $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('view','id'=>$comment->id));
                } 
                else {
                     //Return messages from try/catch
                    echo CJSON::encode(array(
                        'msg' => 'Check your form, and fill up all required fields'
                    ));
                }
                Yii::app()->end();
            }
        }
    }

我的评论对象

Comment Object
(
    [_type:Comment:private] => 
    [_key:Comment:private] => 
    [_new:Comment:private] => 
    [_attributes:CActiveRecord:private] => Array
        (
            [money] => 2
            [reliability] => 2
            [recommend] => 1
            [pending] => 1
            [title] => test!!!!!
            [message] => test!!!!!test!!!!!test!!!!!test!!!!!test!!!!!test!!!!!test!!!!!
        )
    [_related:CActiveRecord:private] => Array
        (
        )
    [_c:CActiveRecord:private] => 
    [_pk:CActiveRecord:private] => 
    [_alias:CActiveRecord:private] => t
    [_errors:CModel:private] => Array
        (
        )
    [_validators:CModel:private] => CList Object
        (
            [_d:CList:private] => Array
                (
                    [0] => CSafeValidator Object
                        (
                            [attributes] => Array
                                (
                                    [0] => message
                                )
                            [message] => 
                            [skipOnError] => 
                            [on] => Array
                                (
                                )
                            [except] => Array
                                (
                                )
                            [safe] => 1
                            [enableClientValidation] => 1
                            [_e:CComponent:private] => 
                            [_m:CComponent:private] => 
                        )
                    [1] => CInlineValidator Object
                        (
                            [method] => validateType
                            [params] => Array
                                (
                                    [on] => create
                                )
                            [clientValidate] => 
                            [attributes] => Array
                                (
                                    [0] => type
                                )
                            [message] => 
                            [skipOnError] => 
                            [on] => Array
                                (
                                    [create] => create
                                )
                            [except] => Array
                                (
                                )
                            [safe] => 1
                            [enableClientValidation] => 1
                            [_e:CComponent:private] => 
                            [_m:CComponent:private] => 
                        )
                    [2] => CInlineValidator Object
                        (
                            [method] => validateKey
                            [params] => Array
                                (
                                    [on] => create
                                )
                            [clientValidate] => 
                            [attributes] => Array
                                (
                                    [0] => key
                                )
                            [message] => 
                            [skipOnError] => 
                            [on] => Array
                                (
                                    [create] => create
                                )
                            [except] => Array
                                (
                                )
                            [safe] => 1
                            [enableClientValidation] => 1
                            [_e:CComponent:private] => 
                            [_m:CComponent:private] => 
                        )
                    [3] => CInlineValidator Object
                        (
                            [method] => validateType
                            [params] => Array
                                (
                                    [on] => create
                                )
                            [clientValidate] => 
                            [attributes] => Array
                                (
                                    [0] => type
                                )
                            [message] => 
                            [skipOnError] => 
                            [on] => Array
                                (
                                    [create] => create
                                )
                            [except] => Array
                                (
                                )
                            [safe] => 1
                            [enableClientValidation] => 1
                            [_e:CComponent:private] => 
                            [_m:CComponent:private] => 
                        )
                    [4] => CInlineValidator Object
                        (
                            [method] => validateKey
                            [params] => Array
                                (
                                    [on] => create
                                )
                            [clientValidate] => 
                            [attributes] => Array
                                (
                                    [0] => key
                                )
                            [message] => 
                            [skipOnError] => 
                            [on] => Array
                                (
                                    [create] => create
                                )
                            [except] => Array
                                (
                                )
                            [safe] => 1
                            [enableClientValidation] => 1
                            [_e:CComponent:private] => 
                            [_m:CComponent:private] => 
                        )
                    [5] => CRequiredValidator Object
                        (
                            [requiredValue] => 
                            [strict] => 
                            [trim] => 1
                            [attributes] => Array
                                (
                                    [0] => recommend
                                )
                            [message] => 
                            [skipOnError] => 
                            [on] => Array
                                (
                                )
                            [except] => Array
                                (
                                )
                            [safe] => 1
                            [enableClientValidation] => 1
                            [_e:CComponent:private] => 
                            [_m:CComponent:private] => 
                        )
                    [6] => CRequiredValidator Object
                        (
                            [requiredValue] => 
                            [strict] => 
                            [trim] => 1
                            [message] => 
                            [skipOnError] => 
                            [on] => Array
                                (
                                )
                            [except] => Array
                                (
                                )
                            [safe] => 1
                            [enableClientValidation] => 1
                            [_e:CComponent:private] => 
                            [_m:CComponent:private] => 
                        )
                    [7] => CRequiredValidator Object
                        (
                            [requiredValue] => 
                            [strict] => 
                            [trim] => 1

                            [message] => 
                            [skipOnError] => 
                            [on] => Array
                                (
                                )
                            [except] => Array
                                (
                                )
                            [safe] => 1
                            [enableClientValidation] => 1
                            [_e:CComponent:private] => 
                            [_m:CComponent:private] => 
                        )
                    [8] => CRequiredValidator Object
                        (
                            [requiredValue] => 
                            [strict] => 
                            [trim] => 1

                            [message] => 
                            [skipOnError] => 
                            [on] => Array
                                (
                                )
                            [except] => Array
                                (
                                )
                            [safe] => 1
                            [enableClientValidation] => 1
                            [_e:CComponent:private] => 
                            [_m:CComponent:private] => 
                        )
                    [9] => CUniqueValidator Object
                        (
                            [caseSensitive] => 1
                            [allowEmpty] => 1
                            [className] => 
                            [attributeName] => 
                            [criteria] => Array
                                (
                                )
                            [message] => 
                            [skipOnError] => 1
                            [on] => Array
                                (
                                )
                            [except] => Array
                                (
                                )
                            [safe] => 1
                            [enableClientValidation] => 1
                            [_e:CComponent:private] => 
                            [_m:CComponent:private] => 
                        )
                    [10] => ENumberPlateValidator Object
                        (
                            [attributes] => Array
                                (
                                    [0] => no_plate
                                )
                            [message] => 
                            [skipOnError] => 
                            [on] => Array
                                (
                                )
                            [except] => Array
                                (
                                )
                            [safe] => 1
                            [enableClientValidation] => 1
                            [_e:CComponent:private] => 
                            [_m:CComponent:private] => 
                        )
                    [11] => CSafeValidator Object
                        (
                            [attributes] => Array
                                (
                                    [0] => id
                                    [1] => message
                                    [2] => userId
                                )
                            [message] => 
                            [skipOnError] => 
                            [on] => Array
                                (
                                    [search] => search
                                )
                            [except] => Array
                                (
                                )
                            [safe] => 1
                            [enableClientValidation] => 1
                            [_e:CComponent:private] => 
                            [_m:CComponent:private] => 
                        )
                )
            [_c:CList:private] => 12
            [_r:CList:private] => 
            [_e:CComponent:private] => 
            [_m:CComponent:private] => 
        )
    [_scenario:CModel:private] => insert
    [_e:CComponent:private] => Array
        (
            [onbeforesave] => CList Object
                (
                    [_d:CList:private] => Array
                        (
                            [0] => Array
                                (
                                    [0] => CTimestampBehavior Object
                                        (
                                            [createAttribute] => createDate
                                            [updateAttribute] => 
                                            [setUpdateOnCreate] => 
                                            [timestampExpression] => 
                                            [_enabled:CBehavior:private] => 1
                                            [_owner:CBehavior:private] => Comment Object
 *RECURSION*
                                            [_e:CComponent:private] => 
                                            [_m:CComponent:private] => 
                                        )
                                    [1] => beforeSave
                                )
                        )
                    [_c:CList:private] => 1
                    [_r:CList:private] => 
                    [_e:CComponent:private] => 
                    [_m:CComponent:private] => 
                )
        )
    [_m:CComponent:private] => Array
        (
            [CTimestampBehavior] => CTimestampBehavior Object
                (
                    [createAttribute] => createDate
                    [updateAttribute] => 
                    [setUpdateOnCreate] => 
                    [timestampExpression] => 
                    [_enabled:CBehavior:private] => 1
                    [_owner:CBehavior:private] => Comment Object
 *RECURSION*
                    [_e:CComponent:private] => 
                    [_m:CComponent:private] => 
                )
        )
    [_new:CActiveRecord:private] => 1
)

错误我得到

 <b>Fatal error</b>:  Class name must be a valid object or a string in <b>/Library/WebServer/Documents/dev/common/lib/yii/framework/db/ar/CActiveRecord.php</b> on line <b>395</b><br />

这是CActiveRecord.php

public static function model($className=__CLASS__)
    {
        if(isset(self::$_models[$className]))
            return self::$_models[$className];
        else
        {
            $model=self::$_models[$className]=new $className(null); //this is line 395
            $model->attachBehaviors($model->behaviors());
            return $model;
        }
    }

在我的例子中,这就是注释的工作方式:

public function actionCreate(){
    $comment = new Comment;
    if($_POST[get_class($comment)]){
        // save data here 
        if ( $comment->save() ) {
            // comment saved !!
        } else {
            // comment not saved
        }
    }
    $this->render('new',array('comment'=>$comment));
}

在您的案例中,注释是一个组件,而不是一个模型
我不知道是否要检查是否提交了表格。

注释模型示例:

class Comment extends CActiveRecord
{
    $id;
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
    public function rules(){
    }
}