Yii Framework Forum: andFilterWhere - Yii Framework Forum

Jump to content

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

andFilterWhere wildcards in LIKE operator Rate Topic: -----

#1 User is offline   jorge sequeira 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 81
  • Joined: 05-February 13
  • Location:PORTUGAL

Posted 01 October 2017 - 09:10 AM

Hello,

I have this code inside my search model:
$query->andFilterWhere(['like', 'name', $this->name])


It generates LIKE '%name% , which is fine in most ocasions.

But I would like my system to be able to replace spaces by %, so I could search for "Homer Simpson" and the system would include "Homer J Simpson" in the result.

How do I set the like operator? I've tried to replace spaces by % inside the searched name without success.

Thank you
0

#2 User is offline   jorge sequeira 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 81
  • Joined: 05-February 13
  • Location:PORTUGAL

Posted 02 October 2017 - 03:16 PM

Many views but no answer...
This question must be too hard, or too easy to deserve an answer!

Anyway, if someone has the same problem... I already figured it out.

Answer:

        $search_name="'%".str_replace(" ","%",$this->name)."%'";
        $query->andWhere('name like '.$search_name)


As far as I was able to understand, you can't use andFilterWhere to do this.
But within andWhere you can build your WHERE clause just like in SQL.
1

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