在进行"搜索"时,显示的行没有任何变化。在yii2

时间:2016-08-24 02:55:57

标签: php web yii2

在yii2中进行modelsearch时遇到问题。这是关系图

enter image description here

我想在表格 aitambah 中显示 Jurusan 到表格 ais3 ,我想在表格中显示 NamaMahasiswa ai ais3 。我创建了表 s3penghubung ,以便表 aitambah 可以与 ais3 相关联。我能够显示它们。但是,当我尝试输入" BIO"在 Jurusan 字段中,搜索无法正常工作。它刷新了页面。显示的行没有任何变化。所以,我该怎么做才能解决这个问题? enter image description here

模型搜索代码

<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Ais3;
use app\models\AiTambah;

/**
 * Ais3Search represents the model behind the search form about `app\models\Ais3`.
 */
class Ais3Search extends Ais3
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'kode'], 'integer'],
            [['NRP', 'NRP1', 'NRP2', 'NRP3', 'NRP4', 'NRP5', 'NRP6', 'NRP7', 'namaMahasiswaText', 'ProgramStudi', 'TanggalMasuk', 'TanggalKeluar', 'jURUSANText','NAMKANTOR','ANGKATAN'], 'safe'],
        ];
    }

    public $NamaMahasiswa;
    public $namaMahasiswaText;
    public $NRP1;
    public $NRP2;
    public $NRP3;
    public $NRP4;
    public $NRP5;
    public $NRP6;
    public $NRP7;
    public $JURUSAN;
    public $jURUSANText;
    public $NAMKANTOR;
    public $ANGKATAN;





    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Ais3::find();
        $query->joinWith('ai');

        // 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;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'kode' => $this->kode,

        ]);




        $query->andFilterWhere(['like', 'ais3.NRP', $this->NRP])
            ->andFilterWhere(['like', 'ai.NamaMahasiswa', $this->namaMahasiswaText])
            ->andFilterWhere(['like', 'ais3.ProgramStudi', $this->ProgramStudi])
            ->andFilterWhere(['like', 'ai.TanggalMasuk', $this->TanggalMasuk])
            ->andFilterWhere(['like', 'ais3.TanggalKeluar', $this->TanggalKeluar])
            ->andFilterWhere(['like', 'aitambah.JURUSAN', $this->JURUSAN]);



        return $dataProvider;
    }
}

MY GRIDVIEW CODE

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

            //'id',
            'NRP',
            'namaMahasiswaText',
            'ProgramStudi',
            //'TanggalMasuk',
            [
            'attribute' => 'TanggalMasuk',
            'value' => function($data) {
                return $data->ai->TanggalMasuk;
            },],
            'TanggalKeluar',

            //YANG DITAMBAH
            /*[
                'attribute'=>'NRP1',
                'value'=>'s3penghubung.aitambah.JURUSAN',
                'label' => 'Jurusan',
            ],*/

            'jURUSANText',

            [
                'attribute'=>'NRP2',
                'value'=>'s3penghubung.aitambah.NAMKANTOR',
                'label' => 'Nama Kantor',
            ],
            [
                'attribute'=>'NRP3',
                'value'=>'s3penghubung.aitambah.ANGKATAN',
                'label' => 'Angkatan',
            ],
            [
                'attribute'=>'NRP4',
                'value'=>'s3penghubung.aitambah.TELP',
                'label' => 'Nomor HP/Telp',
            ],
            [
                'attribute'=>'NRP5',
                'value'=>'s3penghubung.aitambah.PEKERJAAN',
                'label' => 'Pekejaan',
            ],
            [
                'attribute'=>'NRP6',
                'value'=>'s3penghubung.aitambah.EMAIL',
                'label' => 'Email',
            ],
            [
                'attribute'=>'NRP7',
                'value'=>'s3penghubung.aitambah.KODEPROP',
                'label' => 'KodeProp',
            ],


            // 'kode',

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

我的模型&#39;代码

<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "ais3".
 *
 * @property integer $id
 * @property string $NRP
 * @property string $ProgramStudi
 * @property string $TanggalMasuk
 * @property string $TanggalKeluar
 * @property integer $kode
 *
 * @property Ai $nRP
 * @property S3penghubung $kode0
 */
class Ais3 extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'ais3';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['kode'], 'required'],
            [['kode'], 'integer'],
            [['NRP'], 'string', 'max' => 15],
            [['ProgramStudi'], 'string', 'max' => 5],
            [['TanggalMasuk', 'TanggalKeluar'], 'string', 'max' => 20],
            [['NRP'], 'exist', 'skipOnError' => true, 'targetClass' => Ai::className(), 'targetAttribute' => ['NRP' => 'NRP']],
            [['kode'], 'exist', 'skipOnError' => true, 'targetClass' => S3penghubung::className(), 'targetAttribute' => ['kode' => 'kode']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'NRP' => Yii::t('app', 'Nrp'),
            'ProgramStudi' => Yii::t('app', 'Program Studi'),
            'TanggalMasuk' => Yii::t('app', 'Tanggal Masuk'),
            'TanggalKeluar' => Yii::t('app', 'Tanggal Keluar'),
            'kode' => Yii::t('app', 'Kode'),
            'namaMahasiswaText' => Yii::t('app', 'Nama Mahasiswa'),
            'jURUSANText' => Yii::t('app', 'Jurusan'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getai()
    {
        return $this->hasOne(Ai::className(), ['NRP' => 'NRP']);
    }



    /**
     * @return \yii\db\ActiveQuery
     */
    public function getS3penghubung()
    {
        return $this->hasOne(S3penghubung::className(), ['kode' => 'kode']);
    }

    //YANG DITAMBAH
    public function getRelasiS3()
    {
        return array(
                'aitambah'=>array(self::MANY_MANY, 'Aitambah', 'S3Penghubung(AiS3.id, Aitambah.NRP)'),
            );
    }


    public function getNamaMahasiswaText()
    {
        $ai = ai::findOne(['NRP'=> $this->NRP]);
        if (empty($ai))
            return '';
        return $ai->NamaMahasiswa;
    }

    public function getJURUSANText()
    {
        $aitambah = aitambah::findOne(['NRP'=> $this->NRP]);
        if (empty($aitambah))
            return '';
        return $aitambah->JURUSAN;
    }

}

我不知道如何修复这些代码。你能帮我解决这个问题吗?三江源

1 个答案:

答案 0 :(得分:1)

您的代码很难调试。尽量保持命名约定的一致性。 Yii方式是用下划线分隔的所有小写字母。

请阅读thisthis有关显示和排序关系的信息。

至于解决方案,您可以尝试以下内容:

在模型中创建新关系:

/**
 * @return \yii\db\ActiveQuery
 */
public function getAitambah()
{
    return $this->hasOne(Aitambah::className(), ['NRP' => 'NRP']);
}

然后在SearchModel

public function rules()
{
    return [
        [['id', 'kode'], 'integer'],
        [['NRP', 'NRP1', 'NRP2', 'NRP3', 'NRP4', 'NRP5', 'NRP6', 'NRP7', 'namaMahasiswaText', 'ProgramStudi', 'TanggalMasuk', 'TanggalKeluar', 'jURUSANText','NAMKANTOR','ANGKATAN', 'JURUSAN'], 'safe'],
    ];
}

在你的搜索方法中:

...
$query = Ais3::find();
query->joinWith(['ai ai', 'aitambah aitambah']);
...

最后在您看来:

[
    'attribute' => 'JURUSAN',
    'value' => 'aitambah.JURUSAN',
    'label' => 'JURUSAN',
],