Or, if you have an actual attribute for valoare_stoc, you could add a beforeValidate method for the model which triggers it calculating the valoare_stoc value.
Whether you use before/afterValidate or beforeSave would depend on what your rules are for the three fields. Any of the three would do the job, but I’d probably best put it in the afterValidate so that you’re sure the in_stoc and pret_vanzare attributes are good before calculating off them.
If you want it to auto-validate while the form is being filled out, you could add a validator to your model like so:
Custom Validation API Doc
public function rules(){
...
array('valoare_stoc', 'calculate_total'),
...
}
public function calculate_total( $attribute, $params )
{
if( $this->in_stoc !== null && $this->pret_vanzare !== null )
$this->valoare_stoc = $this->in_stoc * $this->pret_vanzare;
}
Since you’re using it for a calculation, not a check, you don’t need to worry about flagging any error with it, assuming that you’ve got other validators before it which ensure that pret_vanzare and in_stoc are valid.
Ok, so IMO use a totally javascript solution to populate the total field.
Add a listener to each of the other two fields, and then onchange call a function which calculates the value of each field into the total field. You don’t need to use an AJAX call to do it, just basic javascript.
Something along the lines of (there’s a more elegant solution I’m sure):
In the Controller where you render the create file or in the view:
// if not already covered
Yii::app()->clientScript->registerCoreScript('jquery');
// Put this common function in the head
Yii::app()->clientScript->registerScript("
function totalValues()
{
var tot = $('#Marfuri_in_stoc').val() * $('#Marfuri_pret_vanzare').val();
$('#Marfuri_valoare_stoc').val( tot );
}
", CClientScript::POS_HEAD);
// This one will appear in the body
Yii::app()->clientScript->registerScript("
$('#Marfuri_in_stoc').change( function(){ totalValues();} );
$('#Marfuri_pret_vanzare').change( function(){ totalValues();} );
");
Im newbie in Yii, I got a problem and need your help. I have created webapp using yii, created model Credit from table in my DB, I have a column named ‘cost’ I want to make query “SELECT SUM(cost) FROM credit” in mysql it returns me result, but how to realize it using Yii and fetch result? THANK YOU for your answers. I have done this :
<?php
$connection=Yii::app()->db;
$sql=‘SELECT SUM(cost) from credit’;
$command=$connection->createCommand($sql);
$dataReader=$command->query();
foreach($dataReader as $row){
echo $row;
}
?>
but it returns me ‘Array’.
Shortly I need the sum of whole column in the bottom and display SUM.
You could do this in MySQL, by writing a view, no problem. However, in my experience, it’s best to use database tables, represented by active records, rather than views. Views can be represented as active records, but, unlike tables, the active records will not have all of the db relationships built in, so they’re less powerful. (That being said, I still have to use views when representing data with a lot of relationships, but I only do so for the “admin” view of the data. Views rarely work for the “update” view, because, with few exceptions, most views are not “updateable”.)
If you do this in php, I believe the code below will work. Take care that the value of the uah column is enclosed in tic marks as shown.
thank you very much!!! one more question, if i want to do the same in my model class? i mean if I enter USD value it automatically fills UAH with USD*8 into database table.
I wish you r not tired of my questions. I ask it on forums because i could not find solutions on internet.