我必须检查其他表的产品数量,并将其显示在当前网格视图中。因此,我在模型中创建了一个函数来获取该列的计数。
但我的问题是我如何排序自定义列(checked_kpl)在网格视图。
这是我的代码。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;
}
注意:- purchaseorderarrival是另一个模型。我已经和这个模型建立了关系。- getCheckedKpl函数是给我的产品数量计数。
VIEW -在VIEW中,我将此代码放在gridview小部件中用于显示列。
array(
'name' => 'checked_kpl',
'value' => '$data->getCheckedKpl()',
'type' => 'raw',
'class' => 'DataColumn'
),
任何帮助将不胜感激。谢谢。
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,
) );
}
}