Yii Framework Forum: Problem with $criteria - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Problem with $criteria Rate Topic: -----

#1 User is offline   yadino 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 04-October 11

Posted 13 April 2012 - 01:22 PM

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?
0

#2 User is offline   wisp 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 192
  • Joined: 04-February 11

Posted 13 April 2012 - 01:26 PM

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

#3 User is offline   yadino 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 04-October 11

Posted 13 April 2012 - 01:40 PM

thx, i've already found and solved the problem!
0

#4 User is online   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,120
  • Joined: 05-January 12
  • Location:Morocco

Posted 13 April 2012 - 01:45 PM

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

#5 User is offline   yadino 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 04-October 11

Posted 13 April 2012 - 01:57 PM

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

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users