简体   繁体   中英

processing data and saving to db in Yii

I'm learning Yii, and for my current project I need to do something like this: I have registration page where users enter their data and send it, Yii saves data exactly as user type it, so if I have field A and B, when user fill them it will be saved in database under A and B columns. what I want to do is to take data from A and B, do some calculations and save output to C column (a and b shouldn't even exist) could You please tell me how to achieve such task?

In model:

public $A;
public $B;

In form:

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

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

In controller:

public function actionLogin()
{
    $model = new Model();

    if (isset($_POST['Model'])) {
        //quite simple exaple
        $model->C = $_POST['Model']['A'] + $_POST['Model']['B'];
        $model->save();
    }

}

Or you can add method beforeSave() to your model, and do somethin like V4KK4R says.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM