auto SUM

I’m not tired of your questions. :mellow: However, most of your questions are covered in the reference book:

If you want to have uah computed in your model, it’s very simple, just

  1. Define the property "uah" in your model

  2. Define a label for it

  3. Override the afterFind() function of the model. As the name suggests, afterFind() gets run after you run find() on any active record:





/** Public property, set by afterFind() */

public $uah = null;


public function attributeLabels()  {

return array(

   'id' => 'ID',

   'usd' = 'USD',

   'uah' = 'UAH',

   // etc.

   );

}


public function afterFind() {

	$this->uah = $this->usd * 8;

	return true;

}




Once you do that you can now refer to "uah" more simply in the view, as you figured out:





$this->widget('zii.widgets.grid.CGridView', array(

        'id'=>'gelen-kasa-grid',

        'dataProvider'=>$model->search(),

        'filter'=>$model,

        'columns'=>array(

                'id',

                'tarih',

                'aciklama',

                'uah',

                'usd',

                array('class'=>'CButtonColumn'),

          ),

)); 




::)

That’s great!

How to sort this "uah" in CGridView?

uah is what I refer to as a "computed column" (borrowing lingo from databases.) To sort by a computed column, you must

  1. Add the column as a searchable field in the rules() function of the model:



public function rules()

{

   return array(

		 array('tarih, aciklama, usd', 'required'),

		 array('usd', 'integerOnly'=>true),

		 // etc.

		 // searchable fields:

		 array('tarih, aciklama, usd, uah', 'safe', 'on'=>'search'),

		 );

}




  1. Add the column as a search criterion in the search() function of your model:



public function search()

{

   $criteria=new CDbCriteria;

   $criteria->compare('tarih',$this->tarih,true);

   $criteria->compare('aciklama',$this->aciklama,true);

   $criteria->compare('usd',$this->usd,true);

   // etc.


   // Allow a user to search by uah value. uah is not a col; it's computed from usd

   // For details on search criteria, see http://tinyurl.com/29sqzaz

   $criteria->addSearchCondition(usd * 8, $this->uah);


   return new CActiveDataProvider(get_class($this), array('criteria'=>$criteria));

}




  1. In your _search.php view, allow users to search by uah:



<div class="row">

   <?php echo $form->label($model,'uah'); ?>

   <?php echo $form->textField($model,'uah'); ?>

</div>




That should work.

Now, if you find yourself wanting to a have a lot of computed columns, it gets far simpler to just

  1. Create a database view

  2. Generate a model of the database view, using gii

Just keep in mind that models created from database views are not as dynamic as models created from database tables. However, they’re ideal (imho) for browsing table (what yii calls the “admin.php” view.) I.e., use them for actionAdmin, but use plain, database-table models for actionUpdate, etc.

Happy yii coding!

::)

When I needed to do this, I used the STAT in relation method:




public function relations() {

    ...

    'contatosCount' => array(self::STAT, 'CategoriaContato', 'id_categoria', 'select' => 'count(1)'),

    ...

}



In this case, I have a counter contacts belonging to a category. is a relationship MANY TO MANY.

Best regards!

Thanks for the update. I haven’t used self::STAT relationship yet; I’ll try it!

:-*

Is using stat in the relations() method as efficient as just using a view, and generating a model based on the view?