SQLSTATE[23000]:违反完整性约束:1052 Column 'name' in where clause is ambiguous .. Yii2
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'name' in where clause is ambiguous .. Yii2
这里我 运行 在 Yii2 gridview 中尝试搜索时出现以下错误
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'name' in where clause is ambiguous
The SQL being executed was: SELECT COUNT(*) FROM `leagues` LEFT JOIN `country` ON
`leagues`.`country_id` = `country`.`id` WHERE `name` LIKE '%Eredivisie%'
下面的 sql 代码是由 Yii 生成的,所以我不知道 sql 错误的原因或如何操作它。下面是我的搜索模型,显示了两者之间的关系 table
<?php
namespace backend\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\League;
class LeagueSearch extends League
{
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id', 'created_at', 'updated_at'], 'integer'],
[['name','country_id'], 'safe'],
];
}
/**
* {@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 = League::find();
$query->joinWith('country');
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort'=> ['defaultOrder' => ['name' => SORT_ASC]],
]);
$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,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
]);
$query->andFilterWhere(['like', 'name', $this->name])
->andFilterWhere(['like', 'country.name',$this->country_id]);
return $dataProvider;
}
}
name 是联盟 table 中的一个列,国家/地区也有一个名为 name 的列,所以我认为 sql 在这里遗漏了一些东西。对此的任何帮助将不胜感激
因为您在 table、country
和 league
中都有 name
列,这就是您收到错误的原因。
Column 'name' in where clause is ambiguous
更改以下行
$query->andFilterWhere(['like', 'name', $this->name])
至
$query->andFilterWhere(['like', '{{%league}}.name', $this->name])
或
为 league
table
分配一个别名
$query = League::find()->alias('l');
并为 league
table 中的 name
列使用该别名,如下所示
$query->andFilterWhere(['like', 'l.name', $this->name])
另一件事是您将 country_id
与国家 name
列进行比较?
andFilterWhere(['like', 'country.name',$this->country_id]);
如果 $this->country_id
中有一个 id,您应该与 country.id
进行比较。
这里我 运行 在 Yii2 gridview 中尝试搜索时出现以下错误
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'name' in where clause is ambiguous
The SQL being executed was: SELECT COUNT(*) FROM `leagues` LEFT JOIN `country` ON
`leagues`.`country_id` = `country`.`id` WHERE `name` LIKE '%Eredivisie%'
下面的 sql 代码是由 Yii 生成的,所以我不知道 sql 错误的原因或如何操作它。下面是我的搜索模型,显示了两者之间的关系 table
<?php
namespace backend\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\League;
class LeagueSearch extends League
{
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id', 'created_at', 'updated_at'], 'integer'],
[['name','country_id'], 'safe'],
];
}
/**
* {@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 = League::find();
$query->joinWith('country');
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort'=> ['defaultOrder' => ['name' => SORT_ASC]],
]);
$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,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
]);
$query->andFilterWhere(['like', 'name', $this->name])
->andFilterWhere(['like', 'country.name',$this->country_id]);
return $dataProvider;
}
}
name 是联盟 table 中的一个列,国家/地区也有一个名为 name 的列,所以我认为 sql 在这里遗漏了一些东西。对此的任何帮助将不胜感激
因为您在 table、country
和 league
中都有 name
列,这就是您收到错误的原因。
Column 'name' in where clause is ambiguous
更改以下行
$query->andFilterWhere(['like', 'name', $this->name])
至
$query->andFilterWhere(['like', '{{%league}}.name', $this->name])
或
为 league
table
$query = League::find()->alias('l');
并为 league
table 中的 name
列使用该别名,如下所示
$query->andFilterWhere(['like', 'l.name', $this->name])
另一件事是您将 country_id
与国家 name
列进行比较?
andFilterWhere(['like', 'country.name',$this->country_id]);
如果 $this->country_id
中有一个 id,您应该与 country.id
进行比较。