我有三个桌子?
s(s#,firstName,lastName)
,p(p#,name)
和sp(s#,p#,qty)
。
s模型(由gii创建)具有所有关系。
代码:
public function actionIndex()
{
$dataProvider = new ActiveDataProvider([
'query' =>s::find(),
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
}
并在索引视图中:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'firstName',
'lastName',
[
'attribute'=>'qty',
'value'=>'sp.qty'
]
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
我想在网格视图中显示数量。但没有显示。 我想在gridview中显示关注查询:
select firstName,lastName,qty from s join sp on s.s#=sp.s#
出什么问题了?
答案 0 :(得分:0)
创建搜索类别
class sSearch extend s {
public $name;
public $qnt;
public functioin search()
{
return new ActiveDataProvider([
'query' =>self::find()
->select([
's.*',
'sp.qnt'
])
->innerJoin('sp','s.s = sp.s'),
]);
}
}
搜索类别添加到操作
public function actionIndex()
{
$search = new sSearch()
return $this->render('index', [
'dataProvider' => $search->search(),
]);
}
答案 1 :(得分:0)
方法1:
public function actionIndex()
{
$dataProvider = new ActiveDataProvider([
'query' =>p::find(),
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
}
在网格视图中:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
//to be declared in PSearch Model
'attribute' => 'first_name',
'value' => function ($data) {
// Relation will be setup in the model
if ($data->sp) {
return $data->sp->s->first_name;
}
return 'Display whatever is required';
},
],
[
//to be declared in PSearch Model
'attribute' => 'last_name',
'value' => function ($data) {
// Relation will be setup in the model
if ($data->sp) {
return $data->sp->s->last_name;
}
return 'Display whatever is required';
},
],
'name',
[
//to be declared in PSearch Model
'attribute' => 'quantity',
'value' => function ($data) {
// Relation will be setup in the model
if ($data->sp) {
return $data->sp->quantity;
}
return 'Display whatever is required';
},
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
如果这没有帮助,请发表评论。谢谢!!
答案 2 :(得分:0)
首先,您应该检查您的关系:
应该是hasOne
而不是hasMany
getSps() {
return $this->hasOne(Sp::className(), ['sId' => 'id']);
}
,您必须与sps
建立联系,而不仅仅是sp
,因为您的联系名称为getSps
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'firstName',
'lastName',
[
'attribute'=>'qty',
'value'=>'sps.qty'
]
['class' => 'yii\grid\ActionColumn'],
],
]); ?>