[英]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. 我正在学习Yii,对于我当前的项目,我需要做这样的事情:我有一个注册页面,用户可以在其中输入数据并发送数据,Yii会根据用户键入的内容保存数据,因此,如果我有字段A和B,当用户填写它们时,它将被保存在数据库的A和B列下。 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?
我想做的是从A和B中获取数据,进行一些计算并将输出保存到C列(a和b甚至不应该存在)请您告诉我如何实现此任务?
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.) 或者,您可以将方法beforeSave()添加到模型中,然后执行类似V4KK4R的操作。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.