Yii Cactivedataprovider

I need to fetch records based on following conditions :

if(isset($corporateid) && !empty($corporateid)) {

$cond1 =” CORPORATEID=$corporateid “;


if(isset($brokerid) && !empty($brokerid)) {

if($cond1!=”){$cond1 .=” AND “;}

$cond1 .=” AGENTID=$brokerid”;


if(isset($RMId) && !empty($RMId)) {

if($cond1!=”){$cond1 .=” AND “;}




$sql=”select *from CRMTOPUPPROPOSERDETAILS where $cond1


The above condition code is written in simple php but i need to create these condition in Yii frame work using CActiveDataProvider;

Pls help me to solve this problem.

read this first:



you will replace the ‘status=1’ with $cond1.

Thanks you for your kind reply Mubashar Iqbal .

Its not working. I need to create these condition using CDbCriteria and then use that criteria with cActiverecords. I have to avoid php code. Please help me.

See i have 5 fields for filter records. User can enter any one or can all 5 fields to be search. so i need to find how many fields has been entered by user to search and then create a search condition. But i must have to use CDbCriteria and CActiveDataProvider for filtering records.

i can create such condition in core php easily but facing problem in yii. i had tried 100 times but i am fail to get.

Hi you can write a custome query on model search function like

public function search() {

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);

        $criteria->compare('firstname', $this->firstname, true);

        $criteria->compare('lastname', $this->lastname, true);

        $criteria->compare('user_type', $this->user_type, true);

        $criteria->compare('email', $this->email, true);

        $criteria->compare('password', $this->password, true);

        //$criteria->compare('city_id', $this->city_id, true);

        if (isset($this->city_id) && !empty($this->city_id)) {

            $criteria = new CDbCriteria;

            $criteria->select = 't.*, tu.* ';

            $criteria->join = ' LEFT JOIN `city` AS `tu` ON t.city_id = tu.id';

            $criteria->addCondition("city_name='" . $this->city_id . "'");


        if (isset($this->categories_id) && !empty($this->categories_id)) {

            $criteria = new CDbCriteria;

            $criteria->select = 't.*, tu.* ';

            $criteria->join = ' LEFT JOIN `categories` AS `tu` ON t.categories_id= tu.id';

            $criteria->addCondition("category_name='" . $this->categories_id . "'");



        $criteria->compare('created_at', $this->created_at, true);

        $criteria->compare('updated_at', $this->updated_at, true);

        $criteria->compare('logdate', $this->logdate, true);

        $criteria->compare('lognum', $this->lognum, true);

        $criteria->compare('is_active', $this->is_active, true);

        $criteria->compare('user_roles', $this->user_roles, true);

        $criteria->compare('extra', $this->extra, true);

        $criteria->compare('username', $this->username, true);

        $criteria->compare('unique_id', $this->unique_id, true);

        $criteria->addCondition('is_active IN ("1","0")');

        return new CActiveDataProvider($this, array(

            'criteria' => $criteria,

            'sort' => UtilityHtml::GetOrder($criteria)



I hope it’s some help.

Thanks for your reply Maggie Q(Ankit Modi) .

This search()will created in Model but how this will call in controller and how the records will be fetched. How this will call in CActiveRecords. Can you please write the code for me you will great help me.

I have a table tab1(tab1_id,mobilNo,name,stateId,cityId,campaignId,leadSourceId,leadStatusId,leadStageId).I need to fetch all the records on the user specific condition on mobilNo,name,leadSourceId,leadStatusId,and using join have to fetch city name,statename from citymaster,statemaster. user can enter any one or can all the 4 fields. then i have to detect the fields enter by user and makes the condition then fetch the records. But condition is that only by using CActiveDataProvider. Please help.

Thanks To all of you to help and guid me in such a irritating situation. Finally i got what i want. thanks.

Hii to all…

i need to remain show the value in text box after clicking on submit button. As we generally do in HTML text box like : <input type=“text” name=“name” value="<?php echo @$_POST[‘name’];?>">.

I want to do the same in yii. I tried to do this as :

<?php echo $form->textField($model,‘name’,array(‘class’=>‘txtfield_new’,‘id’=>‘name’, ‘autocomplete’=>‘off’,‘value’=>$model->name)); ?>

But i did’t get banifit.

Can any body help me. ????

Hi please see it

I hope it’s some help.

You give me the link this will help full when we simply creating CHtml text field. But we are creating text field by model.

Actually the name field is an variable in my model, so when i post this by clicking submit button the fields get blank. But i need it to remain filed the entered value.