2016-08-19 5 views
1

NRPで検索しようとしたときにエラーが発生しました。ここでは ":整合性制約違反句があいまいなところでは1052列 'NRP' SQLSTATE [23000]" がモデルのコードです:SQLSTATE [23000]:整合性制約違反:1052 where句の 'NRP'列が曖昧です

<?php 

namespace app\models; 

use Yii; 

/** 
* This is the model class for table "alumnys2". 
* 
* @property integer $Alumnys2ID 
* @property string $NRP 
* @property string $ProgramStudi 
* @property string $Tanggal_Masuk 
* @property string $Tanggal_Lulus 
* 
* @property AlumniIntegrasi $nRP 
*/ 
class AlumnyS2 extends \yii\db\ActiveRecord 
{ 
    /** 
    * @inheritdoc 
    */ 
    public static function tableName() 
    { 
     return 'alumnys2'; 
    } 

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

    /** 
    * @inheritdoc 
    */ 
    public function attributeLabels() 
    { 
     return [ 
      'Alumnys2ID' => Yii::t('app', 'Alumnys2 ID'), 
      'NRP' => Yii::t('app', 'Nrp'), 
      'ProgramStudi' => Yii::t('app', 'Program Studi'), 
      'Tanggal_Masuk' => Yii::t('app', 'Tanggal Masuk'), 
      'Tanggal_Lulus' => Yii::t('app', 'Tanggal Lulus'), 
      'namaMahasiswaText' => Yii::t('app', 'Nama Mahasiswa'), 
     ]; 
    } 

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

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

} 

、ここではmodelsearchコードです:それは言っ

<?php 

namespace app\models; 

use Yii; 
use yii\base\Model; 
use yii\data\ActiveDataProvider; 
use app\models\AlumnyS2; 
use app\models\AlumniIntegrasi; 

/** 
* AlumnyS2Search represents the model behind the search form about `app\models\AlumnyS2`. 
*/ 
class AlumnyS2Search extends AlumnyS2 
{ 
    /** 
    * @inheritdoc 
    */ 
    public function rules() 
    { 
     return [ 
      [['Alumnys2ID'], 'integer'], 
      [['NRP', 'namaMahasiswaText', 'ProgramStudi', 'Tanggal_Masuk', 'Tanggal_Lulus'], 'safe'], 
     ]; 
    } 

    public $NamaMahasiswa; 
    public $namaMahasiswaText; 

    /** 
    * @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 = AlumnyS2::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([ 
      'Alumnys2ID' => $this->Alumnys2ID, 
     ]); 

     $query->andFilterWhere(['like', 'NRP', $this->NRP]) 
      ->andFilterWhere(['like', 'alumniIntegrasi.NamaMahasiswa', $this->namaMahasiswaText]) 
      ->andFilterWhere(['like', 'ProgramStudi', $this->ProgramStudi]) 
      ->andFilterWhere(['like', 'Tanggal_Masuk', $this->Tanggal_Masuk]) 
      ->andFilterWhere(['like', 'Tanggal_Lulus', $this->Tanggal_Lulus]); 

     return $dataProvider; 
    } 
} 

とここにインデックスがあります:

<?php 

use yii\helpers\Html; 
use yii\grid\GridView; 
use app\models\AlumniIntegrasi; 

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

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

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

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

      //'Alumnys2ID', 
      'NRP', 
      'namaMahasiswaText', 
      'ProgramStudi', 
      'Tanggal_Masuk', 
      'Tanggal_Lulus', 

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

この問題を解決する方法はわかりません。誰かが私にこのコードを解決してください助けてください。

$query->andFilterWhere(['like', 'alumnys2.NRP', $this->NRP]) 
      ->andFilterWhere(['like', 'alumniIntegrasi.NamaMahasiswa', $this->namaMahasiswaText]) 
      ->andFilterWhere(['like', 'alumnys2.ProgramStudi', $this->ProgramStudi]) 
      ->andFilterWhere(['like', 'alumnys2.Tanggal_Masuk', $this->Tanggal_Masuk]) 
      ->andFilterWhere(['like', 'alumnys2.Tanggal_Lulus', $this->Tanggal_Lulus]); 
+0

使用エイリアスを変更することにより:ありがとうそんなに

私の問題は FIXED私はこのようになり、クエリのコードを変更しています。このリンクをチェックアウトしてください。 https://github.com/yiisoft/yii2/issues/2377 –

+0

vijay、上記のコードに基づいて例を挙げてください。 –

+0

私の問題は解決しました。私はクエリコードをこの$ query-> andFilterWhere(['like'、 'alumnys2.NRP'、$ this-> NRP])のように変更します。 - > andFilterWhere(['like'、 'alumniIntegrasi.NamaMahasiswa'、$ ( '' like '、' alumnys2.ProgramStudi '、$ this-> ProgramStudi)) - > andFilterWhere([' like '、' alumnys2.Tanggal_Masuk '、$ this-> Tanggal_Masuk ]) - > andFilterWhere(['like'、 'alumnys2.Tanggal_Lulus'、$ this-> Tanggal_Lulus]); –

答えて

0

ヨールsearchmodelクエリでフィルタクエリに

$query->andFilterWhere([ 
       'tbl_Yourtablename.NRP' => $this->NRP, 
       'tbl_Yourtablename.namaMahasiswaText' => $this->namaMahasiswaText, 
       'tbl_Yourtablename.ProgramStudi' => $this->ProgramStudi, 
       'tbl_Yourtablename.Tanggal_Masuk' => $this->Tanggal_Masuk, 
       'tbl_Yourtablename.Tanggal_Lulus' => $this->Tanggal_Lulus 

      ]); 
+0

よかったよ、ビジェイ。あなたは私の問題を解決しました。できます。 GBU :) –

+0

こんにちはvijay、あなたが与えたコードを試してみましたが、NRPは検索できますが、 "NamaMahasiswa"は検索できません。 alumnys3.NRP '=> $ this-> NRP、 ' alumniintegrasi.NamaMahasiswaText '=> $ this-> NamaMahasiswaText、 ' alumnys3.ProgramStudi '=> $ this-> ProgramStudi、 ' alumnys3.TanggalMasuk '=> $ this - > TanggalMasuk、 'alumnys3.TanggaKeluar' => $ this-> TanggalKeluar –

+0

- > andFilterWhere(['like'、 'alumniIntegrasi.namaMahasiswaText'、$ this-> namaMahasiswaText])、試してみよう –

関連する問題