在 yii2 中连接表时显示结果

Display results while joining tables in yii2

我正在连接两个表 tbl_complain_type 和 tbl_section.The 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 不是正确的方法。我想听听一些建议或更好的方法来实现这一目标。

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

[
   '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,
],