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

Kartik GridView Yii2 : Toggle data not working in toolbar
yii2 http client request and repsponse
How to insert multiple times the value of an attribute in single table in yii2
yii2 SluggableBehavior is applied to only emty slug field
Yii2: Create path and url for folder inside a module
Disable ActiveForm Clientside Validation on button other than submit Yii2
CRUD view link does not work in Yii2
Yii2 DynamicForm: Demo 3: Nested Dynamic Form with types of rooms
Company Follow Button disappear after login popup - Linkedin issue
How do I get rid of composer-asset-plugin in yii2
Yii2 Can not Store huge integer
Yii2 flash messages rendered automatically
Foreign Keys in yii2.0
ActiveForm doesn't submitting after clicking submit Yii2
Does Yii2 AR class can check, does two objects has a link?
Yii2 dependent DropdownList State and City

Categories

HOME
maven
react-virtualized
iot
leon
cross-browser
mouse
constraint-programming
podio
esper
communication
vifm
adobe-analytics
fancybox-3
postgres-xl
static-libraries
leiningen
carthage
designer
reactive-cocoa
introduction
amazonsellercentral
kvc
restful-authentication
crystal-reports-2010
filezilla
replaceall
windows-server-2000
stormpath
preg-match-all
functor
titanium-mobile
fog
android-kernel
rotational-matrices
picasso
wpf-controls
c11
webix-treetable
splice
ensembles
gulp-sourcemaps
no-www
midl
import-from-excel
user-accounts
dds
tropo
color-profile
vtigercrm
specrun
migradoc
google-feed-api
angular-cache
heidisql
removechild
sts-springsourcetoolsuite
phishing
mikroc
flash-cs5
rhino-servicebus
netmq
endeca-workbench
citrus-pay
purge
service-accounts
interrupted-exception
tween
pick
markers
html-helper
aapt
typo3-neos
prettify
jsctypes
eventual-consistency
pushbackinputstream
lcs
simba
bluepill
cascalog
newtonscript
spring-portlet-mvc
hgsubversion
external-accessory
deobfuscation
subgurim-maps
visitor-statistic
yslow
sproutcore-2
user-friendly
adrotator
uimenucontroller
zend-decorators
procedural-music
dbisam

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