这是我在我的控制器中的动作
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;
}
}
答案 0 :(得分:0)
这就是我的案例中评论的方式:
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(){
}
}