I'm new to the Yii framework and now I need to create a search function in Yii2. I already have found a search function in Yii which looks exactly like the funciton I need.
Here is the code:
public function searchDocuments($documentModel, $query, $content=false, $content_only=false) {
$criteria = new CDbCriteria();
$criteria->with = array(
'tags' // Tabel tbl_tag toevoegen via de relations.
);
$criteria->compare('content', $query, true, "OR");
$criteria->compare('description', $query, true, "OR");
$criteria->compare('year', $query, true, "OR");
$criteria->compare('title', $query, true, "OR");
$criteria->compare('tags.slug', $query, true, "OR");
$criteria->compare('title', $documentModel->title, true, "AND");
$criteria->compare('description', $documentModel->description, true, "AND");
$criteria->compare('tags.slug', $documentModel->tag_search, true, "AND");
$criteria->compare('year', $documentModel->year, true, "AND");
$criteria->compare('tags.state', 1 , false, "AND");
$criteria->group = 't.id';
$criteria->together = true;
return new CActiveDataProvider( $documentModel, array(
'pagination'=>array('pageSize'=>25),
'criteria' => $criteria,
'sort'=>array(
'attributes'=>array(
'tag_search'=>array(
'asc'=>'tags.slug',
'desc'=>'tags.slug DESC',
),
'*',
),
),
)
);
}