phpyiicactivedataprovider

CActiveDataProvider doesnt give selected columns


    $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";

It works fine and get all columns which I select. But when I pass it in CActiveDataProvider it gets only this attributes.

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

How I select all columns?


Solution

  • In Yii if you are using as for sql, it needs a public variable to store the value in.

    add all your custom column names as public variables on the model and all should be dandy.

    so if you have $criteria->select = 't.*,count(business_unit.id) as TotalBusinessUnits, this will create the corresponding sql . but Yii does not recognise TotalBusinessUnits as a valid column on the table.

    So if you add public $TotalBusinessUnits; in your model Yii has a place to store this value.

    Also you should then add TotalBusinessUnits to your safe attributes rule so that Yii knows it safe to fetch.

    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,
        ));
    }