Skip to main content
Version: Fegg 5.x (next)

Model::whereLike

Search a column for a term

Description​

public self Model::whereLike( string $column, string $term, [ string $mode = 'contains', string $andOr = 'AND' ] )

Adds column LIKE ? ESCAPE '!'. % and _ in $term match literally, so user input can be passed as it is. Throws InvalidArgumentException when $column is not a plain name or $mode is unknown.

Arguments​

NameData typeDefaultRemarks
$columnstringColumn name
$termstringText to search for
$modestring'contains'Where the term must be (below)
$andOrstring'AND'How it joins the previous condition: AND or OR

Modes​

ModePattern
contains%term%
prefixterm%
suffix%term
exactterm

Example​

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();