yii2 Gridview 不显示具有连接属性的列
yii2 Gridview does not show column with joined attributes
我有一个模型证书,它有另一个模型的外键 application_id,称为应用程序。所以每个证书都属于一个应用程序。
现在有一种情况,我想显示现有用户的所有证书。用户 ID 存在于应用程序模型中,如 user_id.
这是查询
SELECT * FROM `certificates`
inner join applications b ON
application_id = b.id where b.user_id = 7
现在,根据来自上述查询的记录,我想显示证书的一些列和使用网格视图的应用程序的一些列。但由于某些原因,如果记录不止一个,那么我不会从应用程序中获取任何列数据。
<?php Pjax::begin(); ?> <?= GridView::widget([
'dataProvider' => $dataProvider,
// 'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'application_id',
'verified_application_file_path',
'certificate_name',
'application.name',
'application.user_id',
[
'attribute' => 'creation_date',
'format' => ['date', 'php:d/m/Y']
],
[
'attribute' => 'expiry_date',
'format' => ['date', 'php:d/m/Y']
],
],
]); ?>
<?php Pjax::end(); ?></div>
如果单个记录得到 return,上面的网格显示姓名和用户 ID,否则显示 "Not set"。我不确定为什么 'application.name' 和 'application.user_id' 在收到多个记录时不起作用。
这是我使用 yii2 的查询
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search_vendor_certificates($user_id)
{
$query = ApplicationCertificates::find()->joinWith('application b',true , 'INNER JOIN')->where(["b.user_id"=>$user_id]);
// add conditions that should always apply here
$dataProvider = new \yii\data\ActiveDataProvider([
'query' => $query,
]);
return $dataProvider; }
如果有人能告诉我我在显示应用程序模型的正确属性时犯了什么错误,我将不胜感激。
首先(不要用这个,我会告诉你一个逻辑错误):
->joinWith('application b',true , 'INNER JOIN')
U 为 application
b
设置别名,然后在 GridView
中使用 application
。反正在GridView
.
中改名为b
还是不行
基于答案:
模型表二:
public function getTableOneRecord()
{
return $this->hasOne(TableOne::className(), ['id' => 't1_id']);
}
public function getTableThreeRecord()
{
return $this->hasOne(TableThree::className(), ['id' => 't3_id']);
}
public function getTableFourRecord()
{
return $this->hasOne(TableFour::className(), ['id' => 't4_id'])
->via('tableThreeRecord');
}
和视图文件:
echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'id',
't1_id',
'tableOneRecord.id',
't3_id',
'tableThreeRecord.id',
'tableThreeRecord.t4_id',
'tableFourRecord.id',
],
]);
用最简单的方式来说,您的搜索关系在 GridView 中不起作用,您必须在 Model
中定义关系,然后使用它们 ;)
我有一个模型证书,它有另一个模型的外键 application_id,称为应用程序。所以每个证书都属于一个应用程序。
现在有一种情况,我想显示现有用户的所有证书。用户 ID 存在于应用程序模型中,如 user_id.
这是查询
SELECT * FROM `certificates`
inner join applications b ON
application_id = b.id where b.user_id = 7
现在,根据来自上述查询的记录,我想显示证书的一些列和使用网格视图的应用程序的一些列。但由于某些原因,如果记录不止一个,那么我不会从应用程序中获取任何列数据。
<?php Pjax::begin(); ?> <?= GridView::widget([
'dataProvider' => $dataProvider,
// 'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'application_id',
'verified_application_file_path',
'certificate_name',
'application.name',
'application.user_id',
[
'attribute' => 'creation_date',
'format' => ['date', 'php:d/m/Y']
],
[
'attribute' => 'expiry_date',
'format' => ['date', 'php:d/m/Y']
],
],
]); ?>
<?php Pjax::end(); ?></div>
如果单个记录得到 return,上面的网格显示姓名和用户 ID,否则显示 "Not set"。我不确定为什么 'application.name' 和 'application.user_id' 在收到多个记录时不起作用。
这是我使用 yii2 的查询
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search_vendor_certificates($user_id)
{
$query = ApplicationCertificates::find()->joinWith('application b',true , 'INNER JOIN')->where(["b.user_id"=>$user_id]);
// add conditions that should always apply here
$dataProvider = new \yii\data\ActiveDataProvider([
'query' => $query,
]);
return $dataProvider; }
如果有人能告诉我我在显示应用程序模型的正确属性时犯了什么错误,我将不胜感激。
首先(不要用这个,我会告诉你一个逻辑错误):
->joinWith('application b',true , 'INNER JOIN')
U 为 application
b
设置别名,然后在 GridView
中使用 application
。反正在GridView
.
b
还是不行
基于
模型表二:
public function getTableOneRecord()
{
return $this->hasOne(TableOne::className(), ['id' => 't1_id']);
}
public function getTableThreeRecord()
{
return $this->hasOne(TableThree::className(), ['id' => 't3_id']);
}
public function getTableFourRecord()
{
return $this->hasOne(TableFour::className(), ['id' => 't4_id'])
->via('tableThreeRecord');
}
和视图文件:
echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'id',
't1_id',
'tableOneRecord.id',
't3_id',
'tableThreeRecord.id',
'tableThreeRecord.t4_id',
'tableFourRecord.id',
],
]);
用最简单的方式来说,您的搜索关系在 GridView 中不起作用,您必须在 Model
中定义关系,然后使用它们 ;)