SQLSTATE [23000]:完整性约束违规:1052 where子句中的列'NRP'不明确

时间:2016-08-19 08:23:36

标签: php mysql web yii2

当我尝试在NRP中搜索时,出现了错误。它说“SQLSTATE [23000]:完整性约束违规:1052列'NRP'在where子句中是不明确的” 这是模型的代码:

<?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]);

1 个答案:

答案 0 :(得分:0)

通过更改过滤查询

 $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

            ]);