PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, February 10, 2022

[FIXED] How to get the sum of column's data in Yii using query?

 February 10, 2022     php, yii     No comments   

Issue

I want the SUM of data available in the particular column using the Yii query. Here is the code:

$resource_cnt = Resources::model()->findAll(array(
     'select'=>'prj_id, SUM(amount) as amt',
     'condition'=>'prj_id=:prj_id',
     'params'=>array(':prj_id'=>$_POST['Resources']['prj_id']))
);

I tried using the query above. But it did not get the SUM of amt variable.


Solution

The "correct" thing to do it Yii, if you want it nicely do in the model, is to declare a property in Resources called amt. Then it should work with your query. Yii only populates those atrributes from a select query, which it can find in the model.

class Resources.... {

  public $amt;
...

  public yourFunction() {

    $resource_cnt = Resources::model()->findAll(array(
      'select'=>'prj_id, SUM(amount) as amt',
      'condition'=>'prj_id=:prj_id',
      'params'=>array(':prj_id'=>$_POST['Resources']['prj_id']))
    );

    echo $resource_cnt->amt;
  }

...
}


Answered By - Asped
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing