2
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->attributes);

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] => 
)

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

模型属性

/**
 * 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 */
4

1 回答 1

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 请求时保存记录:)。试试看,我希望这就是你要找的。

问候

于 2012-05-16T05:51:21.660 回答