Model::whereLike
カラムを文字列で検索します
説明
public self Model::whereLike( string $column, string $term, [ string $mode = 'contains', string $andOr = 'AND' ] )
column LIKE ? ESCAPE '!'を追加します。$termの中の%と_は文字どおりに一致するので、ユーザーの入力をそのまま渡せます。$columnが単純な名前でない場合や、$modeが不明な場合はInvalidArgumentExceptionを投げます。
引数
| 名前 | データ型 | デフォルト | 備考 |
|---|---|---|---|
| $column | string | カラム名 | |
| $term | string | 検索する文字列 | |
| $mode | string | 'contains' | 文字列が一致する位置(下記) |
| $andOr | string | 'AND' | 前の条件とのつなぎ方(ANDまたはOR) |
モード
| モード | パターン |
|---|---|
contains | %term% |
prefix | term% |
suffix | %term |
exact | term |
例
Controller
$model = new Model(false, array(
'table' => 'cms_news',
'id' => 'news_id',
'visible' => 'visible',
));
// ... Where title LIKE '%50!%%' ESCAPE '!' AND ... (titles containing "50%")
$records = $model->whereLike('title', '50%')->find();
// Titles starting with "News"
$records = $model->whereLike('title', 'News', 'prefix')->find();
// Search title or content
$keyword = $this->in('keyword');
$records = $model
->whereOpen()
->whereLike('title', $keyword)
->whereLike('content', $keyword, 'contains', 'OR')
->whereClose()
->find();