CActiveDataProvider不提供选定的列

时间:2016-03-30 07:20:36

标签: php yii cactivedataprovider

    $criteria->select = 't.*,count(business_unit.id) as TotalBusinessUnits,count(users.id) as TotalUsers ,count(positions.id) as TotalPositions, count(skills.id) as TotalSkills , users.first_name , users.last_name, count(question_bank.id) as TotalQuestions';

    $criteria->condition = "`t`.status = '1'";
    $criteria->group = 't.id';
    $criteria->order = 'created DESC';
    $criteria->join = " inner join users on users.company_id = `t`.id";
    $criteria->join .= " left join business_unit on business_unit.company_id = `t`.id";
    $criteria->join .= " left join billingdetails on billingdetails.company_id = `t`.id";
    $criteria->join .= " left join positions on positions.company_id = `t`.id";
    $criteria->join .= " left join skills on skills.company_id = `t`.id";
    $criteria->join .= " left join question_bank on question_bank.company_id = `t`.id";

它工作正常并获取我选择的所有列。 但是当我在CActiveDataProvider中传递它时,它只获得this个属性。

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

我如何选择所有列?

1 个答案:

答案 0 :(得分:1)

在Yii中,如果你使用as作为sql,它需要一个公共变量来存储值。

将所有自定义列名称添加为模型上的公共变量,所有这些都应该是花花公子。

所以如果你有$criteria->select = 't.*,count(business_unit.id) as TotalBusinessUnits,这将创建相应的sql。但Yii不承认TotalBusinessUnits是表格中的有效列。

因此,如果您在模型中添加public $TotalBusinessUnits;,Yii可以存储此值。

此外,您应该将TotalBusinessUnits添加到您的安全属性规则中,以便Yii知道可以安全获取。

public $TotalBusinessUnits;

public function rules(){
    return array(
        // your rules
        array('TotalBusinessUnits','safe'),
    );
}

public function search(){
    $criteria->select = 't.*,count(business_unit.id) as TotalBusinessUnits';
    // your criteria
    return new CActiveDataProvider($this, array(
        'criteria' => $criteria,
    ));
}