Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在yii2中连接表时显示结果_Yii2 - Fatal编程技术网

在yii2中连接表时显示结果

在yii2中连接表时显示结果,yii2,Yii2,我正在连接两个表tbl_类型和tbl_节 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 ; -- ----------------------

我正在连接两个表tbl_类型和tbl_节

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\u投诉类型
tbl\u区段
具有类似
tbl\u区段
的关系。id=
tbl\u投诉类型
。区段id

我在投诉类型模型中建立了如下关系:

public function getSection()
    {

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

        return $this->hasOne(ComplainType::className(), ['id' => 'section_id']);
    }
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;
    }
在剖面模型中

public function getSection()
    {

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

        return $this->hasOne(ComplainType::className(), ['id' => 'section_id']);
    }
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;
    }
我将ComplainTypeSearch模型修改为

public function getSection()
    {

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

        return $this->hasOne(ComplainType::className(), ['id' => 'section_id']);
    }
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;
    }
因此,在我的索引函数中,用节名而不是节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'],
            ],
        ]); ?>


不打印视图文件中的节。有没有比使用更好的方法
$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,
],