yii2


Perform calculations before recording in the archive


What better way to carry out an operation between two fields and save result to a third column? Must be made actionCreate or beforeSave?
Columns:
Value (user input)
commission (user input)
companys_revenue (just a field)
Example:
value x commission = companys_revenue
I need to get the data that the user entered and apply the formula and write to the database
I did as follows:
public function beforeSave($insert)
{
if ($this->isNewRecord)
{
$companys_revenue = ($this->value*$this->commission_percent)/100;
$this->companys_revenue = abs($companys_revenue);
}
$companys_revenue = ($this->value*$this->commission_percent)/100;
$this->companys_revenue = abs($companys_revenue);
return parent::beforeSave($insert);
}
create a controller :
public function actionCalculation() {
$model = new classname();
if(Yii::$app->request->post()['calculationform']){
$value = Yii::$app->request->post()['calculationform']['value'];
$comission = Yii::$app->request->post()['calculationform']['comission'];
$revenue= Yii::$app->request->post()['calculationform']['revenue'];
$calulation = $model->getFormula($value,$comission,$revenue);
$model->calulation = $calulation;
$model->save();
$this->render('your code');
}
in calculation class :
public $value,$revenue,$comission;
public function getFormula($value,$comission,$revenue){
your calulation write here.....;
}
}
in this way you applied your formula and save that calculation in your database.There is another way also , but try to write logic in model .
i just show you the example you need to take care of the code and other logic also.

Related Links

Yii2 skip to other URL route if <controller>/<action> is not found
Include additional library to yii2
Yii2 make session works on all actions of one controller?
Yii2 display data from phpexcel in view before saving to database
yii2 dependant dropdown reset on edit action due to onchange function
Yii2 non-DB (or virtual) attribute isn't populated during massive assignment?
Yii2 Join multiple tables
Codeception Adding custom assert actions (functional testing)
Selected values in yii2
Yii queryBuilder; If dbexpression is not being quoted well
Time attribute validation in Yii2 framework
Yii2 kartik-datecontrol extension
SQL query help in Yii2
Yii2 Pjax inside ListView not working
how to get post value from multiple array in yii2
I want to display column data in Yii2 listview

Categories

HOME
java
compiler-construction
ngrx
pycharm
kde
mfc
path-finding
plone
hashmap
onedrive
q
rsync
filtering
amazon-ecs
packages
swagger-ui
virtualization
adobe-analytics
midi
size
caml
facebook-page
angular-ui
database-replication
crystal-reports-2008
swingx
tokenize
tar
css-animations
kvc
blazemeter
crosstab
one-to-many
dynamic-featured-image
fifo
msys2
gitignore
quote
bpel
bosh
galsim
karaf
google-qpx-express-api
gtrendsr
sequential
nssegmentedcontrol
az-application-insights
turbogears
businessworks
typescript1.8
pebble-watch
elgg
memory-alignment
epson
watchconnectivity
ableton-live
adxstudio-portals
multi-level
underscore.js-templating
asp.net-4.5
hill-climbing
qcustomplot
testng-dataprovider
javax.sound.midi
django-unittest
spidermonkey
embedded-code
myo
streambase
npapi
camanjs
terminfo
htmlcleaner
mysql-error-1062
datagridviewcolumn
openlaszlo
uv-mapping
easy-install
ora-00911
pushbackinputstream
anonymous-methods
automount
parametric-equations
subscript
netdna-api
inotifycollectionchanged
telerik-scheduler
web-architecture

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App