Problem Adding Criteria In Model Search

I have the following code. It basically ignores the $criteria in the search and just returns the data from the user’s search criteria in the “Advanced Search” form of the CGridView form. I need to have some additional controls put on the data but have been testing and playing with this all afternoon.

Any advice would be greatly appreciated!




$model = new Site('searchSites');

 	$model->unsetAttributes();

//

	$params = Yii::app()->user->getState('SiteSearchParams');

	if ( isset($params) ) {

		$model->attributes = $params;

	}

	$criteria = new CDbCriteria;

	$criteria-> addCondition (" active=1 ");


	$dataProvider = $model->searchSites($criteria);

	$sites = $dataProvider->getData();






searchSites() is a model function that is the same as search but for a limited number of columns.

Dear My Friend

Can you please reveal the code inside the method searchSite?.

Please also putforth your model rules.

Regards.

Hi Seenivasan!

Hope all is well in that part of the world.

My model has a standard search and a modified version to include a ManyMany relation to categories (which creates its own problems in the AR and CGridView with paging due to duplicates.





  public function searchSites() {


	$criteria = new CDbCriteria;  

        $criteria->with = array('SiteCategories');

	$criteria->compare('SiteCategories.category_id', $this->category_id);


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

       	$criteria->compare('active', $this->active)

        $criteria->compare('status_id', $this->status_id);

        $criteria->compare('quality_id', $this->quality_id);

        $criteria->compare('comscore', $this->comscore);

        $criteria->together = true;


        return new CActiveDataProvider(get_class($this), array(

 		'pagination'=>array(

        	'pageSize'=> Yii::app()->user->getState('pageSize',User::model()->myAccount()->find()->pagesize),

    		    ), 

                'criteria' => $criteria,

                ));

    }

    

Rules for Site look like





public function rules() {

        return array(

            array('publisher_id, site_name, site_url, category_id', 'required'),

            array('leadowner_id, network_coord_id, active, status_id, quality_id,  problem', 'default', 'setOnEmpty' => true, 'value' => null),

            array('active, display_live, mobile_live, video_live, mobile_enabled, problem, premium_site, remnant_site', 'numerical', 'integerOnly' => true),

            array('publisher_id, leadowner_id, network_coord_id, category_id, appnexus_id, comscore', 'length', 'max' => 11),

            array('site_name, site_url', 'length', 'max' => 100),      

            array('status_id', 'length', 'max' => 20),

            array('quality_id, premium_type, remnant_type', 'length', 'max' => 2),           

            array('registered_date, signed_date, live_date, sentto_network_date', 'safe'),

            array('id, publisher_id, site_name, site_url, leadowner_id, network_coord_id, rev_share_pct, asked_cpm, floor_cpm, active, status_id, quality_id, category_id, audit_status, problem', 'safe', 'on' => 'search'),

        );



Dear Friend

We have created a new scenario.




$model = new Site('searchSites');



It seems we have to add a rule for that scenario.




array('id,active,status_id,quality_id,comscore','safe','on'=>'searchSites'),



Hopefully it will solve the issue.

Regards.

I missed that totally! Thank you ! I will give it a try!

I modified the select to include the following:





$criteria-> addCondition (" active=1 and id NOT IN (select website_id from partner_site where partner_id = ".$id);




I did this to avoid duplicating data in the list for the partner. Unfortunately it still did it!

It seems to ignore the $criteria in

       $dataProvider = $model->searchSites($criteria); 

Is there a conflict between using $criteria and

the parameters selected by the user? The parameters saved to the user state seem to work perfectly.




$params = Yii::app()->user->getState('SiteSearchParams');

if ( isset($params) ) {

		$model->attributes = $params;

	}



Dear Friend

Your method serchSites has been declared without any arguments.

Then proper signature would be like this.




public function searchSites($criteria) { //added an argument


        //$criteria = new CDbCriteria;  You have to remove it...

        $criteria->with = array('SiteCategories');

        $criteria->compare('SiteCategories.category_id', $this->category_id);


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

        $criteria->compare('active', $this->active)

        $criteria->compare('status_id', $this->status_id);

        $criteria->compare('quality_id', $this->quality_id);

        $criteria->compare('comscore', $this->comscore);

        $criteria->together = true;


        return new CActiveDataProvider(get_class($this), array(

                'pagination'=>array(

                'pageSize'=> Yii::app()->user->getState('pageSize',User::model()->myAccount()->find()->pagesize),

                    ), 

                'criteria' => $criteria,

                ));

    }

    



That worked !

Thank you my friend!