Bhavin Thummar
Bhavin Thummar

Reputation: 1293

Not getting alias in select function in Yii2 from result of query response

My code is below. Issue is that when i have print_r the $data then i am not getting the total_points.

$query = IdsAwardEntries::find()
                ->select(['ids_award_entries.*','SUM(ids_award_points.i_award_points) AS total_points']) 
                ->join('LEFT JOIN', IdsAwardPoints::tableName(), 'ids_award_points.ids_award_entry_id=ids_award_entries.id')
                ->where([
                    'ids_award_entries.deleted_at' => null,
                    'ids_award_entries.i_shortlist_status' => 1,
                ])
                ->groupBy('ids_award_entries.id')
                ->all()
                ;

Please help me where i am wrong. How can i extract total_points value from the response?

Also For your information, i am getting response without the error. When i apply sorting function on total_points then it's working very well according ASC and DESC order.

Upvotes: 1

Views: 53

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133360

You are using an ActiveModel IdsAwardEntries and in this case show only the data related to activeModel ..

if you want see also the total_points alias for sum a simple way is add this name as a public var in the IdsAwardEntries model

class IdsAwardEntries extends \yii\db\ActiveRecord
{
    public $total_points;  
    .....

Upvotes: 2

Related Questions