2

I have to check no of products quantity from other table and display it in current grid view. So i created one function in model for getting count of that column.

But my question is How can I sort that custom column (checked_kpl) in grid view.

Here is my code.

MODEL

public function search() {
    $criteria = new CDbCriteria;

    $criteria->compare('id', $this->id, true);
    $criteria->compare('purchase_order_id', $this->purchase_order_id);
    $criteria->compare('product_id', $this->product_id);
    $criteria->compare('unit_price', $this->unit_price, true);
    $criteria->compare('qty', $this->qty, true);
    $criteria->compare('cost_price', $this->cost_price, true);
    $criteria->compare('customs_percent', $this->customs_percent, true);
    $criteria->compare('discount_percent', $this->discount_percent, true);
    $criteria->compare('notes', $this->notes, true);
    $criteria->compare('created_at', $this->created_at, true);
    $criteria->compare('updated_at', $this->updated_at, true);

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




public function getCheckedKpl() {
    $checked_kpl = 0;
    if (!empty($this->purchaseOrderArrivals)) {
        foreach ($this->purchaseOrderArrivals as $eachArrival) {
            $checked_kpl += $eachArrival->qty;
        }
    }
    return $checked_kpl;
}

NOTE: - purchaseOrderArrivals is another model. I already set relation with this model. - getCheckedKpl function is giving me count of product quantity.

VIEW - In view I put this code in gridview widget for display column.

    array(
            'name' => 'checked_kpl',
            'value' => '$data->getCheckedKpl()',
            'type' => 'raw',
            'class' => 'DataColumn'
         ),

Any help will appreciate. thanks.

1 Answer 1

1
class Model extends CActiveRecord {
  // Adding attribute to work with SQL query
  public $checked_kpl;

  public function attributeLabels(){
    // Prettify column name
    return array( 'checked_kpl' => 'Checked kpl' );
  }

  public function search() {
    $criteria = new CDbCriteria;
    // Count subquery like this
    // SELECT COUNT(*) as checked_kpl, id FROM {{table}} GROUP BY param
    // Condition like this
    // ( q1.id=t.id )
    $criteria->join = "LEFT JOIN (/* **HERE IS A COUNT SUBQUERY** */) as q1 ON(/* **HERE IS A CONDITION** */)";
    $criteria->select = array( '*', new CDbExpression("q1.checked_kpl as checked_kpl") );

    // ... your criteria here

    // Adding custom sort data
    $sort = new CSort();
    $sort->attributes = array(
        'checked_kpl' => array(
            'asc' => 'q1.checked_kpl',
            'desc' => 'q1.checked_kpl DESC'
        )
    );

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

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.