Yii Framework Forum: Addcolumncondition Method With Like Instead Of = (Equal) - Yii Framework Forum

Jump to content

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

Addcolumncondition Method With Like Instead Of = (Equal) Rate Topic: -----

#1 User is offline   Azy 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 156
  • Joined: 16-May 12

Posted 16 December 2013 - 11:57 PM

Hi there,

I have a need like this using addColumnCondition()


foreach($query_set as $query){
    foreach($attributes as $attribute=>$v){
        $attributes[$attribute] = $query;
    }
    $criteria->addColumnCondition($attributes, 'OR', 'AND');
}



this generates,

(attr1=:ycp0 OR attr2=:ycp1) AND (attr1=:ycp2 OR attr2=:ycp3)


but i need it be with LIKE instead of equal condition.

i.e
 (attr1 LIKE %:ycp0% OR attr2 LIKE %:ycp1%) AND (attr1 LIKE %:ycp2% OR attr2 LIKE %:ycp3%)


I found according to yii's default behavior this is not allowed yet. can i know if there is another way to get this done ?

my solution was as below,


class AppCriteria extends CDbCriteria {

   public function addColumnCondition($columns, $columnOperator = 'AND', $operator = 'AND', $like = true) {
      $params = array();
      foreach ($columns as $name=>$value) {
 		if ($value === null)
            $params[] = $name.' IS NULL';
 		else {
            if ($like)
   			$params[] = $name.' LIKE %'.self::PARAM_PREFIX.self::$paramCount.'%';
            else
   			$params[] = $name.'='.self::PARAM_PREFIX.self::$paramCount;
            $this->params[self::PARAM_PREFIX.self::$paramCount++] = $value;
 		}
      }
      return $this->addCondition(implode(" $columnOperator ", $params), $operator);
   }
}


REF : http://www.yiiframew...ondition-detail

i am just wondering why this is not provided by yii still. let me know if this can be achieved in a different way.

Thanks
0

#2 User is offline   Azy 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 156
  • Joined: 16-May 12

Posted 17 December 2013 - 12:02 AM

Sorry by mistake i have post this question in the wrong area. i created a new under Yii 1 discussions.

http://www.yiiframew...stead-of-equal/
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