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

Handle Image update in yii2
Header and Footer missing in modal popup
Select2 Krajee widget - using modal and getting the id of the value
sjaakp/yii2-illustrated-behavior Yii2 extension for image not working
change the navigation bar url from yii2 without redirecting
Yii 2 - Conditional Assets?
How to get value of dropdown instead of id yii2
How can I access private value from object
How to add valueless attribute to <form> tag using yii2 ActiveForm
Register a js file independently in Assets
Get user's email from twitter login in yii2
get Attribute Label in yii2
Yii 2 code formater to display xml with code highlighting in html, string highlighter
Change Breadcrumb URL for one module in yii2
yii2 multi step form
Codeception: No tests executed

Categories

HOME
ms-access
xamarin
testng
vbscript
hook
reflection
angular-material
stock
filtering
spring-cloud-stream
yarn
jxls
retrofit
baqend
qore
awesome-wm
primary-key
google-translate
imacros
row
resize
vb.net-2010
hex-editors
visual-studio-cordova
django-simple-history
ghc
google-static-maps
evopdf
textfield
grails3
csrf-protection
quote
uisplitview
semantic-versioning
openoffice.org
http-digest
event-driven
reportbuilder
opshub
eigenvalue
node-sass
businessworks
galleria
wso2carbon
qwt
rainbowtable
tizen-native-app
android-cursor
namecoin
linode
nbconvert
redundancy
crosswalk-runtime
deadbolt-2
thrust
google-web-starter-kit
xml-attribute
plottable.js
angstrom-linux
dlna
historian
vhd
maven-tomcat-plugin
lemon
python-3.2
purge
wyam
ionic
census
python-green
ng-animate
c3
rdtsc
wordpress-theme-customize
java-metro-framework
titanium-modules
html-helper
document-classification
openexr
article
starcluster
htmlcleaner
transcoding
ember-charts
algebraic-data-types
terminal-services
c18
dir
dataadapter
random-seed
interface-orientation
newtonscript
windows-phone-7.1.1
dotnethighcharts
asyncfileupload
locationlistener
appender
google-friend-connect
subviews
thunderbird-lightning
virtual-functions
avatar
temporal-database
sector

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