Yii没有在表中保存新添加的列

时间:2014-03-25 09:46:41

标签: php yii

在我的Web应用程序中,我有一个名为producer offer的表,我需要再添加两列,它显示添加的列但不保存输入的值 。但当我查看这个值时,它在视图中显示为“未设置”。我该怎样解决这个问题?

public function rules()
    {

        return array(
            array(' vegetable_id, offered_qty, unit_cost, unit_delivery_cost', 'required'),
            array(' offered_qty, unit_cost, unit_delivery_cost, booking_status, booked_by, available_days', 'numerical', 'integerOnly'=>true),
            array('user_id', 'length', 'max'=>11),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id,userName,user_id, vegetable_id, offered_qty, unit_cost, unit_delivery_cost, offered_date, booking_status, booked_by, available_days', 'safe', 'on'=>'search'),
            //array('booked_qty','on'=>'update')
            array('booked_qty,available_qty','numerical', 'safe'=>true),
        );
    }
public function attributeLabels()
    {
        return array(
            'producerOfferUserRelation.name' => 'Offered By',
            'producerOfferVegetableRelation.name' => 'Offered Vegetable',
            'offered_qty' => 'Offered Qty(/KG)',
            'unit_cost' => 'Unit Cost(RS/KG)',
            'unit_delivery_cost' => 'Unit Delivery Cost(RS/KM)',
            'offered_date' => 'Offered Date',
            'booking_status' => 'Booking Status',
            'booked_by' => 'Booked By',
            'available_days' => 'Available Days',
            'booked_qty'=>'Booked quantity',
            'available_qty'=>'Available quantity'
        );
    }

我的ProducerOfferController.php代码

public function actionUpdate($id)
    {
        $model=$this->loadModel($id);
        $booked_qty=$model->booked_qty;
    if(isset($_POST['ProducerOffer']))
    {
        $model->booked_qty=$_POST['ProducerOffer']['booked_qty'];

        $model->available_qty=$model->offered_qty-$model->booked_qty;

        $model->attributes=$_POST['ProducerOffer'];
        $model->save();
        if ($model->hasErrors() === false)
        {
            $this->redirect(Yii::app()->user->returnUrl);
        }
    }
    else
    {
        Yii::app()->user->setReturnUrl($_GET['returnUrl']);
    }
    $this->render('form',array('model'=>$model,));
}

1 个答案:

答案 0 :(得分:0)

更改

    $model->booked_qty=$_POST['ProducerOffer']['booked_qty'];
    $model->available_qty=$model->offered_qty-$model->booked_qty;
    $model->attributes=$_POST['ProducerOffer'];

    $model->attributes=$_POST['ProducerOffer'];
    $model->booked_qty=$_POST['ProducerOffer']['booked_qty'];
    $model->available_qty=$model->offered_qty-$model->booked_qty;
相关问题