Yii2字段过滤器未显示在GridView上,与3个表相关

时间:2017-03-06 12:59:04

标签: gridview yii2 inner-join

我使用gii工具创建了crud应用程序。我有3张桌子'Empreendy','Tramity','Protoprocess'。 gridview工作正常,显示所有必要的数据,但搜索字段'nomeempreend'没有出现。

[Protoprocess table]

id (*)      |  int
============|=======
codprocesso |  char
============|=======
empreendy_id|  int
============|=======
dtabertura  |  date

[Tramity table]

id (*)         |  int
===============|=======
protoprocess_id|  int
===============|=======
datacad        |  date

[Empreendy Table]

id (*)       |  int
=============|=======
clientesy_id |  char
=============|=======
empreendy_id |  int
=============|=======
address      |  char

Index fail

 <?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
       // 'protoprocess_id',
// #update here  < ####
     [
           'attribute' => 'empreendiment',
       'label'=>'Empreendimento',
       'value' => 'empreendyName.empreendy.nomeempreend',
           'filter'=>ArrayHelper::map(Empreendy::find()->asArray()->all(), 'id', 'nomeempreend'),
        ],

//#end here&lt; ####

[
            'attribute' => 'protoprocess_id',
            'value'    => 'protoprocess.codprocesso',
        ],

=======

class TramitySearch extends Tramity
{
/**
 * @inheritdoc
 */

public $empreendy_id;
public function rules()
{
    return [
        [['id'], 'integer'],
        [['obs', 'orgars_id', 'datacad', 'statusproto', 'protoprocess_id', 'empreendy_id', 'Dbusers_id'], 'safe'],
    ];
}


 public function search($params)
{
    $query = Tramity::find();

    // add conditions that should always apply here

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    $this->load($params);

    if (!$this->validate()) {
        // uncomment the following line if you do not want to return any records when validation fails
        // $query->where('0=1');
        return $dataProvider;
    }
$query->joinWith('orgars');
$query->joinWith('protoprocess');
$query->joinWith('empreendyName');
    // grid filtering conditions
    $query->andFilterWhere([
        'id' => $this->id,
     //   'protoprocess_id' => $this->protoprocess_id,
     //   'orgars_id' => $this->orgars_id,
        'datacad' => $this->datacad,
   'empreendyName.empreendy.nomeempreend' => $this->empreendy_id,

  //        'Dbusers_id' => $this->Dbusers_id,
    ]);

    $query->andFilterWhere(['like', 'obs', $this->obs])
        ->andFilterWhere(['like', 'statusproto', $this->statusproto])
        ->andFilterWhere(['like', 'codprocesso', $this->protoprocess_id])
        //    ->andFilterWhere(['like',  'nomeempreend', $this->empreendy_id])
          ->andFilterWhere(['like', 'sigla', $this->orgars_id]);

====  public $ empreendiment;

====      / **      * @return \ yii \ db \ ActiveQuery      * /      公共函数getOrgars()      {         返回$ this-&gt; hasOne(Orgars :: className(),['id'=&gt;'orgars_id']);      }

 /**
 * @return \yii\db\ActiveQuery
 */
  public function getProtoprocess()
{
    return $this->hasOne(Protoprocess::className(), ['id' => 'protoprocess_id'])->with(['empreendy']);
}

public function getDbusers()
{
    return $this->hasOne(Dbusers::className(), ['id' => 'Dbusers_id']);
}

public function getDeptoOrgars()
{
    return $this->hasMany(DeptoOrgars::className(), ['orgars_id' => 'id']);
}

public function getSeqTramity()
{
    return $this->hasMany(SeqTramity::className(), ['tramity_id' => 'id']);
} 



public function getEmpreendyName()
{
return $this->hasOne(Protoprocess::className(), ['id' => 'protoprocess_id'])->with(['tramity']);
}

=====

如何通过'nomeempreend'继续输入查询?

1 个答案:

答案 0 :(得分:0)

只能搜索rules()中的字段,因此:

在您的TramitySearch课程

public function rules()
{
    return [
        [['protoprocess.empreendy_id'], 'integer'],
        [['protoprocess_id'], 'integer'],
    ]
}

让我知道是否适合你,就像为我工作一样!