【问题标题】:SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'NRP' in where clause is ambiguousSQLSTATE [23000]:违反完整性约束:1052 where 子句中的列 'NRP' 不明确
【发布时间】:2016-08-19 08:23:35
【问题描述】:

当我尝试在 NRP 中进行搜索时,出现了错误。它说“SQLSTATE [23000]:违反完整性约束:where 子句中的 1052 列‘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;
    }

}

这里是模型搜索代码:

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

【问题讨论】:

  • 在您的搜索模型查询中使用别名。结帐此链接.. github.com/yiisoft/yii2/issues/2377
  • vijay,你能根据我上面的代码举个例子吗?
  • 我的问题已解决。我将查询代码更改为这样 $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]);

标签: php mysql web yii2


【解决方案1】:

通过更改过滤器查询

 $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

            ]);

【讨论】:

  • 非常感谢维杰。你解决了我的问题。有用。 GBU :)
  • 你好 vijay,当我尝试您提供的代码时,可以搜索 NRP,但无法搜索“NamaMahasiswa”。我该怎么办?我尝试了此代码 $query->andFilterWhere( [ 'alumnys3.NRP' => $this->NRP, 'alumniintegrasi.NamaMahasiswaText' => $this->NamaMahasiswaText, 'alumnys3.ProgramStudi' => $this->ProgramStudi, 'alumnys3.TanggalMasuk' => $this- >TanggalMasuk, 'alumnys3.TanggaKeluar' => $this->TanggalKeluar
  • ->andFilterWhere(['like', 'alumniIntegrasi.namaMahasiswaText', $this->namaMahasiswaText]),试试这个
  • 很高兴收到您的来信,它有效.. :) 请接受我的回答.. 这样其他人也可以联系到这个解决方案.. thnx... 享受 :)
猜你喜欢
  • 2021-10-11
  • 2022-11-24
  • 1970-01-01
  • 1970-01-01
  • 2019-02-22
  • 2021-08-16
  • 2022-08-18
  • 2021-02-05
  • 2019-10-03
相关资源
最近更新 更多