Yii Framework Forum: Yii Model Search, Cdbcriteria With Compare And Condition - Yii Framework Forum

Jump to content

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

Yii Model Search, Cdbcriteria With Compare And Condition Rate Topic: -----

#1 User is offline   Chardard 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 01-November 12

Posted 19 November 2012 - 04:42 AM

Hello

i am currently trying to improve my search functionality by allowing users to find items added to the database between two dates i am doing this using the following code:


		$criteria=new CDbCriteria;
		$criteria->with = array( 'customer', 'userAssign');

		$criteria->compare('taskID',$this->taskID);
		$criteria->compare('createdBy',$this->createdBy);
		$criteria->compare('statusID',$this->statusID,true);
		$criteria->compare('priorityID',$this->priorityID);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('updated',$this->updated,true);
		$criteria->compare('closed',$this->closed,true);
		$criteria->compare('assignedUser',$this->assignedUser);
		$criteria->compare('subject',$this->subject,true);
		$criteria->compare('Description',$this->Description,true);
		$criteria->compare('Email',$this->Email);
		$criteria->compare('unassigned',$this->unassigned,true);

		if(!empty($this->date_from) && !empty($this->date_to)){
		  $criteria->condition="`created` Between '$this->date_from' And '$this->date_to'";
		}



Which is working fine but when a date_from and date_to is chosen on the search form other compare values entered are ignored such as is you choose a status. Am i right in thinking that you cannot mix cdbcriteria compare and condition? and if this is the case how else can this be achieved?

Thanks in advance
0

#2 User is offline   Chardard 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 01-November 12

Posted 19 November 2012 - 05:13 AM

Ok so i have now changed it to this:

		if(!empty($this->date_from) && !empty($this->date_to)){
		  $criteria->addCondition("`created` Between '$this->date_from' And '$this->date_to'", 'AND');
		}


Which seems to work ok...................
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