Yii2 按外国 table 字段和总计数搜索
Yii2 search by foreign table field and total count
我正在尝试通过外国table搜索如下:
2 tables:
人数:
id
name
...
url:
id
peopleID
url
People.php 型号:
public function getUrls()
{
return $this->hasMany(Urls::className(), ['peopleID' => 'id'])->select(['url']);
}
PeopleSearch.php 型号:
...
$query->joinWith(['urls']);
...
$query
->andFilterWhere(['or',
['like', 'name', $this->name],
...
['like', 'url', $this->name]]
);
这适用于在多个字段中搜索在 "name" 字段中输入的值,包括外国 url 一个,但在我看来,我使用类似以下内容的方式输入手动分页:
$dataProvider->prepare();
if ($dataProvider->totalCount > 0)
echo Yii::t('app', 'Showing').": <b> ".($dataProvider->pagination->page*$dataProvider->pagination->pageSize+1)."-".($dataProvider->pagination->page*$dataProvider->pagination->pageSize+$dataProvider->count)."</b> ".Yii::t('app', 'of')." <b>".$dataProvider->totalCount."</b> ".Yii::t('app', 'items');
else echo Yii::t('app', 'No results found.');
echo LinkPager::widget(['pagination' => $dataProvider->pagination])
和 $dataProvider->totalCount
给了我加入 table 的记录总数,但不是 people
的记录总数。例如,如果我在 people
table 中有 2 条记录并且每个记录在 "url" table index.php 中有 20 url 视图显示 "showing 1-2 of 40 items" 而不是 "showing 1-2 of 2 items"
另外 LinkPager::widget
显示的总页数有误
请注意,$dataProvider
是通过
从控制器传递到视图的
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
我该怎么做才能按我想要的方式执行分页?
提前谢谢你,
在 People.php 模型中,我的建议是删除 ->select(['url'])
:
public function getUrls()
{
return $this->hasMany(Urls::className(), ['peopleID' => 'id']);
}
这样您仍然可以在需要时操作这些网址。
在PeopleSearch.php型号中:
...
// $query->joinWith(['urls']);
// This line is the one that makes it so you get 20 results instead of 2, because you actually get one result for each url related to the people returned by the query.
$query->with(['urls']);
// This last line makes sure the model class populates the relation using only one query.
// Two queries will end up being executed to populate both the people and url models,
// however you will get the right amount for $dataProvider->totalCount.
...
if(strlen($this->url) > 0) {
$urlsPeopleIDs = \app\models\Url::find()
->select('peopleID')
->asArray()
->where(['like', 'url', $this->url])
->all();
$query->andWhere(['id' => $urlsPeopleIDs]);
}
// This way you will only filter by url when you receive a url string with lenght > 0.
// If you haven't already, you will need to create a public property called 'url' in your PeopleSearch.php model and add a 'string' or 'safe' rule so you can actually load it's value from post.
我正在尝试通过外国table搜索如下:
2 tables:
人数:
id
name
...
url:
id
peopleID
url
People.php 型号:
public function getUrls()
{
return $this->hasMany(Urls::className(), ['peopleID' => 'id'])->select(['url']);
}
PeopleSearch.php 型号:
...
$query->joinWith(['urls']);
...
$query
->andFilterWhere(['or',
['like', 'name', $this->name],
...
['like', 'url', $this->name]]
);
这适用于在多个字段中搜索在 "name" 字段中输入的值,包括外国 url 一个,但在我看来,我使用类似以下内容的方式输入手动分页:
$dataProvider->prepare();
if ($dataProvider->totalCount > 0)
echo Yii::t('app', 'Showing').": <b> ".($dataProvider->pagination->page*$dataProvider->pagination->pageSize+1)."-".($dataProvider->pagination->page*$dataProvider->pagination->pageSize+$dataProvider->count)."</b> ".Yii::t('app', 'of')." <b>".$dataProvider->totalCount."</b> ".Yii::t('app', 'items');
else echo Yii::t('app', 'No results found.');
echo LinkPager::widget(['pagination' => $dataProvider->pagination])
和 $dataProvider->totalCount
给了我加入 table 的记录总数,但不是 people
的记录总数。例如,如果我在 people
table 中有 2 条记录并且每个记录在 "url" table index.php 中有 20 url 视图显示 "showing 1-2 of 40 items" 而不是 "showing 1-2 of 2 items"
另外 LinkPager::widget
显示的总页数有误
请注意,$dataProvider
是通过
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
我该怎么做才能按我想要的方式执行分页?
提前谢谢你,
在 People.php 模型中,我的建议是删除 ->select(['url'])
:
public function getUrls()
{
return $this->hasMany(Urls::className(), ['peopleID' => 'id']);
}
这样您仍然可以在需要时操作这些网址。
在PeopleSearch.php型号中:
...
// $query->joinWith(['urls']);
// This line is the one that makes it so you get 20 results instead of 2, because you actually get one result for each url related to the people returned by the query.
$query->with(['urls']);
// This last line makes sure the model class populates the relation using only one query.
// Two queries will end up being executed to populate both the people and url models,
// however you will get the right amount for $dataProvider->totalCount.
...
if(strlen($this->url) > 0) {
$urlsPeopleIDs = \app\models\Url::find()
->select('peopleID')
->asArray()
->where(['like', 'url', $this->url])
->all();
$query->andWhere(['id' => $urlsPeopleIDs]);
}
// This way you will only filter by url when you receive a url string with lenght > 0.
// If you haven't already, you will need to create a public property called 'url' in your PeopleSearch.php model and add a 'string' or 'safe' rule so you can actually load it's value from post.