Problem with $criteria

Privet!

I have such code:


public function search($category='')

    {


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

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

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


        if(is_numeric($this->yearFrom)){

            $criteria2 = new CDbCriteria;

            $criteria2->condition='year>=:from';

            $criteria2->params=array(':from'=>$this->yearFrom);

            $criteria->mergeWith($criteria2);

        }


        if(is_numeric($this->yearTo)){

            $criteria2 = new CDbCriteria;

            $criteria2->condition='year<=:to';

            $criteria2->params=array(':to'=>$this->yearTo);

            $criteria->mergeWith($criteria2);

        }


        if(is_numeric($this->costFrom)){

            $criteria2 = new CDbCriteria;

            $criteria2->condition='cost>=:from';

            $criteria2->params=array(':from'=>$this->costFrom);

            $criteria->mergeWith($criteria2);

        }


        if(is_numeric($this->costTo)){

            $criteria2 = new CDbCriteria;

            $criteria2->condition='cost<=:to';

            $criteria2->params=array(':to'=>$this->costTo);

            $criteria->mergeWith($criteria2);

        }

        return new CActiveDataProvider($this, array(

            'criteria'=>$criteria,

            'pagination'=>array(

                'pageSize'=>30,

            ),

            'sort'=>array(

                'defaultOrder'=>'posttime DESC',

            )

        ));

    }

}



The problem is when I enter “yearFrom” and “yearTo” the search is done in the way I need, but when I add “cost to” and “costFrom”, years are already paid no attention and there’s no filtration by years. How can I fix it?

Maybe have a look in the logs so you know the query that is executed?

thx, i’ve already found and solved the problem!

@yadino how please? I was curious when I saw your post. Maybe you forgot to declare costFrom and costTo in your model?

the problem was that for year and for the price was used :from , so i had :form switched from price to year.

Bad:


$criteria2->condition='year>=:from';

            $criteria2->params=array(':from'=>$this->yearFrom);


$criteria2->condition='cost>=:from';

            $criteria2->params=array(':from'=>$this->costFrom);

Solution:


$criteria2->condition='year>=:fromyear';

            $criteria2->params=array(':fromyear'=>$this->yearFrom);


$criteria2->condition='cost>=:fromcost';

            $criteria2->params=array(':fromcost'=>$this->costFrom);