sosow 2011-08-12 09:43:02 4743次浏览 9条回复 0 0 0

事情是这样的,很诡异

我在view中用

<div class="row">
    <?php echo $form->labelEx($model,'cate_id'); ?>
    <?php echo $form->textField($model, 'cate_id');?>
    <?php echo $form->error($model,'cate_id'); ?>
</div>

时,能save()成功并重定向,

但是如果不用textField,而用联动下拉框

<?php 
echo CHtml::dropDownList('pid', '', ProductCategory::getTopCategory(), 
    array(
        'empty'=>'--please select-',
        'ajax'=>array(
            'type'=>'POST',
            'url'=>Yii::app()->createUrl('productCategory/dynamicCate'),
            'update'=>'#c2id',
    ),
));?>
<?php echo CHtml::dropDownList('c2id', '', array('empty'=>'--please select-'));?>

时,create方法没成功执行,页面没重定向,也没存进数据库,应该也就是 if($model->save()) 通不过,两种方式有什么诡异的不同,求解答。万分感谢!

controller中的actionCreate()

/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate($pid)
{
    $model=new RecoProduct;

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['RecoProduct']))
    {
        $model->attributes=$_POST['RecoProduct'];
        $model->product_id = $pid;
        [b]if(isset($_POST['c2id']))
            $model->cate_id = $_POST['c2id'];[/b]
        if($model->save())
            $this->redirect(array('view','id'=>$model->product_id));
    }

    $this->render('create',array(
        'model'=>$model,
    ));
}

model中只重写了beforeSave()

public function beforeSave()
{
    if(parent::beforeSave())
    {
        if($this->isNewRecord)
        {
            $this->is_pass = $this->is_reco = 0;
            $this->reco_time = date("Y-m-d H:i:s",time());
        }
        return true;
    }
    else
        return false;
}
您需要登录后才可以回复。登录 | 立即注册