情况: CausalType 1 -> N原因
进入causaltype的管理视图,我正在使用cgridview,并且我必须显示每个causalType的causala的数量。
我将关系设置为CausalType
return array(
"causals" => array (self::HAS_MANY, "Causal", "causalTypeId" ),
);我添加了class变量
public $activeCausalCount; 这是admin视图中的列
array (
'name' => 'activeCausalCount',
'value' => 'count($data->causals)',
), 实际上,这是我在search()中的条件
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id,true);
$criteria->compare('name',$this->name,true);
$criteria->compare('isActive',$this->isActive);每种类型的因果计数都是正确的,但我有一些问题
1)我只需要计算有效的因果数(计算causals.isActive =1的因果数)
2)我需要对列进行排序
3)我需要过滤(按整数)
发布于 2012-10-15 22:20:01
如果你真的需要对计数进行排序和过滤,那么这可能是一个很小的大过程。
一种方法是..。
向CausalType表中添加一列(称为activeCausals )
在CausalType模型中定义关系
"totalActiveCasuals" => array(
self::STAT,
"Causal",
"causalTypeId",
'condition'=>'totalActiveCasuals.isActive=1'
), 并在因果关系中定义了afterSave方法
protected function afterSave()
{
$this->causaltype->activeCausals = $this->causaltype->totalActiveCasuals;
$this->causaltype->save();
return parent::afterSave();
}现在你可以非常容易地对新的列activeCausals进行过滤和排序。
发布于 2012-10-15 21:52:08
向类型为STAT的CasualType添加一个新关系,如下所示:
return array(
"casuals" => array (self::HAS_MANY, "Causal", "causalTypeId" ),
"totalCasuals" => array (self::STAT, "Causal", "causalTypeId" ),
"totalActiveCasuals" => array (self::STAT, "Causal", "causalTypeId", 'condition' => 'active = true' ),
);然后,在您的视图中,只需将其用作普通属性/关系
https://stackoverflow.com/questions/12894848
复制相似问题