我有一个表格,其中包含有关这些网站的网址和信息。每个网址可以出现一次或两次。该网站的第一次和最后一次扫描。我想在网格视图中显示数据,通过网址启用搜索和第一次扫描的等级以及是否存在第二次扫描等级。当我加载所有我得到正确的数据。网址,等级,等级2。但是当我尝试按年级搜索时,我只获得两次扫描的网站,因此它们在数据库中只有两次,而在数据库中只有一次被忽略。
table:
id url initial points grade
1 blue.com 1 10 F
2 red.com 1 20 F
3 blue.com 0 50 C
etc...
display in gridview works fine
no url points grade points2 grade2
1 blue.com 10 F 50 C
2 red.com 20 F not set not set
etc...
但是当我按网址搜索或评分时,它只显示在表格中进行第二次扫描的结果
¸在模特:
public function getParent() {
return $this->hasOne(self::classname(), ['url' => 'url'])->
from(self::tableName() . ' AS parent')-
>onCondition(['parent.initial'=>0]);
}
public function getParentid(){
return $this->parent->id;
}
public function getParentgrade(){
return $this->parent->grade;
}
public function getParentpoints(){
return $this->parent->points;
}
在搜索模型中:
public $parentid;
public $parentpoints;
public $parentgrade;
public function rules()
{
return [
[[........'parentpoints'.........], 'integer'],
[['url', 'parentgrade', .........], 'safe'],
];
}
public function search($params)
{
$query = SitesTemp::find();
$query->where(['sites_temp.for_scan'=>0, 'sites_temp.initial'=>1]);
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->setSort([
'attributes'=>[
'url',
'points',
'grade',
'parentgrade'=>[
'asc'=>['parent.grade'=> SORT_ASC],
'desc'=>['parent.grade'=> SORT_DESC],
'label'=>'Last Grade'
],
'parentpoints'=>[
'asc'=>['parent.points'=> SORT_ASC],
'desc'=>['parent.points'=> SORT_DESC],
'label'=>'Last Points'
],
]
]);
$this->load($params);
if (!($this->load($params) && $this->validate())) {
$query->joinWith(['parent']);
return $dataProvider;
}
$query->andFilterWhere([
'sites_temp.id' => $this->id,
'sites_temp.initial' => $this->initial,
'sites_temp.points' => $this->points,
]);
$query->andFilterWhere(['like', 'sites_temp.url', $this->url])
->andFilterWhere(['like', 'sites_temp.grade', $this->grade])
->andFilterWhere(['like', 'sites_temp.points', $this-
>points]);
$query->joinWith(['parent'=>function($q){
$q->where('parent.grade like "%'.$this->parentgrade.'%"');
}]);
return $dataProvider;
}
}
答案 0 :(得分:0)
我明白了。
我只有在搜索参数存在时才需要加入joinWith。所以
if(!empty($this->parentgrade)){
$query->joinWith([parent=>function($q){
$q->where('parent.grade like "%'.$this->parentgrade.'%"');
}]); }
解决了我的问题