在yii2中连接表时显示结果

时间:2015-01-15 05:05:48

标签: yii2

我正在加入两个表tbl_complain_type和tbl_section。这些表的sql是

CREATE TABLE IF NOT EXISTS `tbl_complain_type` (
`id` int(9) NOT NULL,
  `section_id` varchar(250) NOT NULL,
  `complains` varchar(250) NOT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=20 ;

-- --------------------------------------------------------

--
-- Table structure for table `tbl_section`
--

CREATE TABLE IF NOT EXISTS `tbl_section` (
`id` int(9) NOT NULL,
  `section` varchar(250) CHARACTER SET latin1 NOT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `tbl_complain_type`
--
ALTER TABLE `tbl_complain_type`
 ADD PRIMARY KEY (`id`);

--
-- Indexes for table `tbl_section`
--
ALTER TABLE `tbl_section`
 ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `tbl_complain_type`
--
ALTER TABLE `tbl_complain_type`
MODIFY `id` int(9) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=20;
--
-- AUTO_INCREMENT for table `tbl_section`
--
ALTER TABLE `tbl_section`
MODIFY `id` int(9) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=8;

tbl_complain_typetbl_section的关系类似tbl_section。id = tbl_complain_type。section_id。

我在ComplainType模型中建立了关系

public function getSection()
    {

        return $this->hasMany(Section::className(), ['id' => 'section_id']);
    }

在剖面模型中

public function getComplainType()
    {

        return $this->hasOne(ComplainType::className(), ['id' => 'section_id']);
    }

我将ComplainTypeSearch模型修改为

public function search($params) {
    $query = ComplainType::find() ;  
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);


     $dataProvider->setSort([
        'attributes' => [
            'id',
            'complains' => [
                'asc' => ['complains' => SORT_ASC, 'complains' => SORT_ASC],
                'desc' => ['complains' => SORT_DESC, 'complains' => SORT_DESC],
                'label' => 'complains',
                'default' => SORT_ASC
            ],
            'section' => [
                'asc' => ['tbl_section.id' => SORT_ASC],
                'desc' => ['tbl_section.id' => SORT_DESC],
                'label' => 'Section'
            ]
        ]
    ]);

    if (!($this->load($params) && $this->validate())) {

        $query->joinWith(['section']);
        return $dataProvider;
    }

    $this->addCondition($query, 'id');
    $this->addCondition($query, 'complains', true);
    $this->addCondition($query, 'section_id');
    return $dataProvider;
    }

所以在我的索引函数中,要显示带有节名而不是section_id

的节

我必须写为

  <?= GridView::widget([
            'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
            'columns' => [
                ['class' => 'yii\grid\SerialColumn'],

                'id',

                ['attribute' => 'section',
                'label'=>'section',
                'format' => 'raw',
                'value'=>function ($data) {
                return    $data['section'][0]->section     ;



                }, 'filter' => true,
                ],
                'complains',

                ['class' => 'yii\grid\ActionColumn'],
            ],
        ]); ?>

,而

<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'id',
             'section',
             ,
            'complains',

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>

不在视图文件中打印部分。有没有比使用更好的方法  $data['section'][0]->section ;显示该部分?我认为使用$data['section'][0]->section不是正确的方法。我想听听一些建议或更好的方法来实现这一目标。

1 个答案:

答案 0 :(得分:1)

您只显示第一部分,如果记录有多个部分,则仅显示第一部分。如果它没有部分,那么你应该看到一个错误。可能你应该做这样的事情

[
   'attribute' => 'section',
   'label'=>'section',
   'format' => 'raw',
   'value'=>function ($model) {
      $sections = [];
      if($model->section) {
         foreach($model->section as $section) {
             $sections[] = $section->section
         }
      }
      return implode(', ', $sections);
   }, 
   'filter' => true,
],