在yii2中搜索不起作用

时间:2016-08-20 01:17:16

标签: php mysql web yii2

我想在 Nama Mahasiswa 搜索栏中搜索,如下图所示。例如,我输入名称 yuhara ,但没有结果,而数据库中有 yuhara

enter image description here

enter image description here

搜索型号代码:

<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\AlumnyS3;
use app\models\AlumniIntegrasi;
/**
 * AlumnyS3Search represents the model behind the search form about `app\models\AlumnyS3`.
 */
class AlumnyS3Search extends AlumnyS3
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['Alumnys3ID'], 'integer'],
            [['NRP', 'NamaMahasiswa', 'ProgramStudi', 'TanggalMasuk', 'TanggalKeluar'], 'safe'],
        ];
    }
    public $NamaMahasiswa;

    /*public $TanggalMasuk;
    public $tanggalMasukText;*/
    /**
     * @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 = AlumnyS3::find();
        $query->joinWith('alumniIntegrasi');

        // 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([
            'Alumnys3ID' => $this->Alumnys3ID,
        ]);



            $query->andFilterWhere([
                'alumnys3.NRP' => $this->NRP,
                'alumniintegrasi.NamaMahasiswa' => $this->NamaMahasiswa,
                'alumnys3.ProgramStudi' => $this->ProgramStudi,
                'alumnys3.TanggalMasuk' => $this->TanggalMasuk,
                'alumnys3.TanggaKeluar' => $this->TanggalKeluar

            ]);

        return $dataProvider;
    }
}

型号代码:

<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "alumnys3".
 *
 * @property integer $Alumnys3ID
 * @property string $NRP
 * @property string $ProgramStudi
 * @property string $TanggalMasuk
 * @property string $TanggalKeluar
 *
 * @property AlumniIntegrasi $nRP
 */
class AlumnyS3 extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'alumnys3';
    }

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

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

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

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

    /*public function getTanggalMasukText()
    {
        $alumniIntegrasi = alumniIntegrasi::findOne(['NRP'=> $this->NRP]);
        if (empty($alumniIntegrasi))
            return '';
        return $alumniIntegrasi->TanggalMasuk;
    }*/
}

索引代码:

<?php

use yii\helpers\Html;
use yii\grid\GridView;

/* @var $this yii\web\View */
/* @var $searchModel app\models\AlumnyS3Search */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('app', 'Alumny S3s');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="alumny-s3-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <p>
        <?= Html::a(Yii::t('app', 'Create Alumny S3'), ['create'], ['class' => 'btn btn-success']) ?>
    </p>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            //'Alumnys3ID',
            'NRP',
            'NamaMahasiswa',
            'ProgramStudi',
            //'tanggalMasukText',
            'TanggalMasuk',
            'TanggalKeluar',

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

我不知道如何解决这个问题。你能帮我解决这个问题吗?如果你能解决这个问题,我真的很感激,谢谢!

1 个答案:

答案 0 :(得分:0)

在搜索模型中添加like查询,如下所示

<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\AlumnyS3;
use app\models\AlumniIntegrasi;
/**
 * AlumnyS3Search represents the model behind the search form about `app\models\AlumnyS3`.
 */
class AlumnyS3Search extends AlumnyS3
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['Alumnys3ID'], 'integer'],
            [['NRP', 'NamaMahasiswa', 'ProgramStudi', 'TanggalMasuk', 'TanggalKeluar'], 'safe'],
        ];
    }
    public $NamaMahasiswa;

    /*public $TanggalMasuk;
    public $tanggalMasukText;*/
    /**
     * @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 = AlumnyS3::find();
        $query->joinWith('alumniIntegrasi');

        // 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([
            'Alumnys3ID' => $this->Alumnys3ID,
        ]);



            $query->andFilterWhere([
                'alumnys3.NRP' => $this->NRP,
               // 'alumniintegrasi.NamaMahasiswa' => $this->NamaMahasiswa,
                'alumnys3.ProgramStudi' => $this->ProgramStudi,
                'alumnys3.TanggalMasuk' => $this->TanggalMasuk,
                'alumnys3.TanggaKeluar' => $this->TanggalKeluar

            ]);
               $query->andFilterWhere(['like', 'alumniintegrasi.NamaMahasiswa', $this->NamaMahasiswa]);



        return $dataProvider;
    }
}