Php $model->save()给我一个错误
我正在尝试建立一个评论系统。当我的表单发布后,调用我的“actionCreate”函数。我在firebug上出错了。但是数据存储在我的数据库中。知道我做错了什么吗 这是我在控制器中的操作Php $model->save()给我一个错误,php,yii,Php,Yii,我正在尝试建立一个评论系统。当我的表单发布后,调用我的“actionCreate”函数。我在firebug上出错了。但是数据存储在我的数据库中。知道我做错了什么吗 这是我在控制器中的操作 public function actionCreate() { /** @var Comment $comment */ $comment = Yii::createComponent($this->module->commentModelCl
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 />
在我的案例中,这就是评论的工作方式:
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(){
}
}
第395行是哪一行?@andrewsi更新了问题。你确定self:$\u models[$className]=new$classnamefull;返回有效对象?谢谢。您正在使用self::$\u模型-$\u模型似乎在您包含的任何代码中都没有被引用;你确定它在范围内吗?对不起,我不太明白$comment是一个对象,并且它确实保存到db的这一行$comment->save请记住comment是models文件夹下的一个模型,扩展了CActiveRecord…此外,该错误意味着autoload找不到comment模型。谢谢,我正在尝试编辑一个扩展以使其更好地符合我的规格。对Yii来说还是新鲜事。
class Comment extends CActiveRecord
{
$id;
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function rules(){
}
}