Yii Framework Forum: Using "OR" in Relational Active Record - Yii Framework Forum

Jump to content

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

Using "OR" in Relational Active Record Rate Topic: -----

#1 User is offline   Sander 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 169
  • Joined: 03-November 09
  • Location:Amsterdam, Netherlands

Posted 06 November 2009 - 09:47 AM

I'm implementing a search page in a social netwrking app, and I want to do a global search for whether a user or one of its related attributes contain the specified search term.

Basically, I want the combined result of the following RAR queries:
User::model()->with(array('field_values'=>array('condition'=>'value LIKE :term', 'params'=>array('term'=>'%'.$model->term.'%'))))->findAll();

User::model()->findAll(array('condition'=>'name LIKE :term', 'params'=>array('term'=>'%'.$model->term.'%')));


Is it possible to combine these two queries into one? Later on I need to search for even more properties and it seems a bit overkill to me to have to do each of these queries separately, when it could be achieved using a simple 'OR' operator in a native SQL query.
0

#2 User is offline   Sander 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 169
  • Joined: 03-November 09
  • Location:Amsterdam, Netherlands

Posted 09 November 2009 - 05:33 AM

For the record, I solved this using CDbCriteria:

$dbCriteria = new CDbCriteria();
$dbCriteria->addSearchCondition('name', $model->term);
$dbCriteria->addSearchCondition('field_values.value', $model->term, true, 'OR');
$results = User::model()->with('field_values')->findAll($dbCriteria);


Eagerly loading the field_values table in this case is essential because otherwise there will be problems searching in its table columns (default is lazy loading, so it won't be joined at all)
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