Я пытаюсь сделать систему комментариев. когда моя форма публикуется и вызывает мою функцию ‘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(){
}
}
Других решений пока нет …