$model->save() not work normally [ 新手入门 ]
事情是这样的,很诡异
我在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;
}
共 9 条回复
- 
 - 
 - 
 - 
 - 
 - 
 - 

 - 
 - 
bubifengyun 回复于 2015-03-04 18:40 举报这么链接都无法打开啊???有同样的问题。
 
sosow Guangzhou
            注册时间:2011-05-31
最后登录:2019-11-06
在线时长:2小时50分
    最后登录:2019-11-06
在线时长:2小时50分
- 粉丝4
 - 金钱100
 - 威望0
 - 积分120