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

How to add a css class to <td> tags in a kv- GridView class in Yii2
Capcha always show incorrect
Integration Reportico in Yii2 Projects
Yii2 : Subdirectory in yii2 not working
Yii2 KCFinder with CKEditor
Yii2 - validation compare acting strange
Updating composer throws exception Class Fxp\Composer\AssetPlugin\Repository\NpmRepository does not exist
Dynamic declaration of a model in Yii2
image are save in folder but not saved in database in yii2.0
how can i put sidebar menu in right side using yii2
Yii2 afterSave. Using changedAttributes show error
how to do a nested foreach loop in yii2?
Yii2, Set multiple Value Select2
update single field directly gave value in update function yii2
How can i use multi controller in one page?
PHP Composer “require” OR condition with version in future

Categories

HOME
hive
mfc
objectgears
getelementsbytagname
cmd
electronics
cakephp-2.5
add
gorm
adobe-analytics
user-input
windows-azure-storage
messages
fallback
serilog
pugjs
predix
amazonsellercentral
telerik-reporting
restful-authentication
trading
firebase-crash-reporting
su
virtualdub
info.plist
linkerd
jaxb2
webkitspeechrecognition
preg-match
protovis
phpfreechat
mmenu
mpmediaquery
html5-fullscreen
fusionpbx
host
suricata
service-discovery
twitch
theano.scan
lumberjack
sfdc
password-encryption
eclipse-gef
executenonquery
cubic-spline
revapi
atomicity
promela
celery-task
ionicons
typescript1.8
nativeapplication
kbuild
mediaelement
specrun
visual-c++-2008
rotativa
underscore.js-templating
asp.net-4.5
simplewebrtc
master-slave
javax.sound.midi
qtableview
metaclass
atk4
map-projections
responsive-images
apache-commons-fileupload
web-controls
proj4js
dereference
geonetwork
java.util.concurrent
xojo
heisenbug
jquery-layout
datagridviewcolumn
meteor-velocity
apc
gridcontrol
rdoc
parametric-equations
asyncfileupload
adk
inotifycollectionchanged
gdlib
yslow
gallio
dmx512
thunderbird-lightning
firefox-5
compiler-specific
sef
anti-piracy
ubuntu-9.04
data-acquisition
3gp

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