在表单提交中插入多个记录 - Yii

时间:2012-05-15 13:30:32

标签: php yii

public function actionCreate()
{
    $model=new events;      


    if(isset($_POST['events']))
    {

        $model->event_title         =   $_POST['events']['event_title'];
        $model->event_description   =   $_POST['events']['event_description'];
        $model->event_repeat_option =   $_POST['events']['event_repeat_option'];
        $model->event_start_date    =   date('Y-m-d H:i:s');
        $model->event_end_date      =   date('Y-m-d H:i:s');
        $model->user_id             =   yii::app()->user->getState('user_id');
        $model->deal_id             =   "1";
        $model->event_type          =   "1";
        $model->event_creation_date =   date('Y-m-d H:i:s');    


        if($model->save())
            $this->redirect(array('view','id'=>$model->event_id));
    }
}

插入多条记录?为什么? 没有错误,没有错误......数据正在完全插入......但是一次只有多个记录。

我做了Print_r($ model->属性);

Array
(
    [event_status] => 1
    [event_title] => test1
    [event_description] => test1
    [event_repeat_option] => test1
    [event_start_date] => 2012-05-15 18:45:40
    [event_end_date] => 2012-05-15 18:45:40
    [user_id] => 3
    [deal_id] => 1
    [event_type] => 1
    [event_creation_date] => 2012-05-15 18:45:40
    [event_id] => 
)

......那里的一切都很好,但仍有一些bug ......无法跟踪它。有什么建议吗?

模型属性

/**
 * This is the model class for table "user_events".
 *
 * The followings are the available columns in table 'user_events':
 * @property integer $event_id
 * @property integer $user_id
 * @property string $event_type
 * @property integer $deal_id
 * @property string $event_creation_date
 * @property string $event_start_date
 * @property string $event_end_date
 * @property string $event_title
 * @property string $event_description
 * @property integer $event_repeat_option
 *  *
 * The followings are the available model relations:
 * @property Deals $deal
 * @property RepeatOptions $eventRepeatOption
 * @property Users $user */

1 个答案:

答案 0 :(得分:3)

我认为这是因为AJAX验证巫婆正在向createAction发送请求(在您的情况下)。您可以在表单窗口小部件中关闭ajax验证,如:

    $form = $this->beginWidget('CActiveForm', array(
        'id'=>'your-form',
        'enableAjaxValidation'=>false, //<- this is important      
    ));

但是如果你真的需要ajax验证,你可以检查控制器中的ajax请求,如:

public function actionCreate()
{
$model=new events;      


if(isset($_POST['events']))
{
    ... //Your stuff. Assigning attributes etc.
    if(!Yii::app()->request->isAjaxRequest){
        if($model->save())
            $this->redirect(array('view','id'=>$model->event_id));
    }
}

}

在这种情况下,它只会在不是ajax请求时保存记录:)。尝试一下,我希望这就是你要找的东西。

此致