对计算字段进行筛选和排序

时间:2014-04-22 05:01:17

标签: yii

美好的一天!这样的问题 学生有一个table:StudID,StudSurName,StudLastName,...
带分数的相关表:PointID,PointStudFK(外键),Points(分) 关系Has_many。

'points' => array(self::HAS_MANY, 'Points', array('PointStudFK' => 'StudID'))

我为每个学生提供了CGridView的积分,但过滤器不起作用 过滤器有效,但会将该值与学生所有分数的总和进行比较,而不是每个分数。

虚拟领域

private $_point= null;

Setter和getter

getSumPoints () - function calculating the sum of scores on individual student

    public function getPoint()
    {
        if ($this->_point === null && $this->points!== null)
        {
            $this->_point = $this->points->getSumPoints($this->StudID);
        }
        return $this->_point;
    }
    public function setPoint($value)
    {
        $this->_point = $value;
    }

search()

    $criteria = new CDbCriteria;
    $criteria->together = true;
    $criteria->with = array('points'=>array(
    'select' => array('SUM(Points) as total')));
    if(!empty($this->pointExp1)) $criteria->having = "total = {$this->point}";

需要分组,但不知道如何。

0 个答案:

没有答案