class
ArticleSearch
extends
Article
{
#关联查询1:这里加上被关联字段
public
$class_name
;
...
public
function
rules()
{
return
[
[[
'id'
,
'article_addtime'
,
'article_updatetime'
],
'integer'
],
[[
'article_title'
,
'article_content'
,
'article_class'
,
'article_author'
],
'safe'
],
#关联查询2:这里加上safe验证,表示该表单字段无验证规则
[
'class_name'
,
'safe'
],
];
}
...
public
function
search(
$params
)
{
$query
= Article::find();
#关联查询4:使用jionWith和select做关联查询
$query
= Article::find();
$query
->joinWith([
'articleClass'
]);
$query
->select(
"`article`.*,`article_class`.class_name"
);
$dataProvider
=
new
ActiveDataProvider([
'query'
=>
$query
,
]);
$this
->load(
$params
);
if
(!
$this
->validate()) {
return
$dataProvider
;
}
#精确查询
$query
->andFilterWhere([
'id'
=>
$this
->id,
'article_addtime'
=>
$this
->article_addtime,
'article_updatetime'
=>
$this
->article_updatetime,
#关联查询5:添加被关联字段的精确查询,这里要跟view表单被查询属性一致,
]);
#模糊查询
$query
->andFilterWhere([
'like'
,
'article_title'
,
$this
->article_title])
->andFilterWhere([
'like'
,
'article_content'
,
$this
->article_content])
->andFilterWhere([
'like'
,
'article_class'
,
$this
->article_class])
->andFilterWhere([
'like'
,
'article_author'
,
$this
->article_author])
#关联查询5:添加被关联字段的精确查询,这里要跟view表单被查询属性一致,
->andFilterWhere([
'like'
,
'`article_class`.class_name'
,
$this
->class_name]);
return
$dataProvider
;
}
...
}