Skip to content

Instantly share code, notes, and snippets.

@wakasann
Last active November 18, 2016 04:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wakasann/ccf5b0ecd8c36fae6c9b005de1592e0b to your computer and use it in GitHub Desktop.
Save wakasann/ccf5b0ecd8c36fae6c9b005de1592e0b to your computer and use it in GitHub Desktop.
yii2 where or and like
<?php
// where status='Open' and category='1' and subCategory='1' and (category like '%test%' or subCategory like '%test%' or title like '%test%' or introduction like '%test%')
//@link http://stackoverflow.com/a/31558035/5840474 get and or
//@link http://stackoverflow.com/a/28593956/5840474 get like
$where = array(
array(
'and',
"status='Open'"
)
);
if(!empty($category)){
$where[0][] = "category='$category'";
}
if(!empty($subCategory)){
$where[0][] = "subCategory='$subCategory'";
}
if(!empty($word)){
$where[] = array(
'or',
array('like','category',$word),
array('like','subCategory',$word),
array('like','title',$word),
array('like','introduction',$word),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment