i have two table person an user and person is linked with user,both have first_name now in search model, i using relation where it is giving me error Column ‘first_name’ in where clause is ambiguous.
i have two table person an user and person is linked with user,both have first_name now in search model, i using relation where it is giving me error Column ‘first_name’ in where clause is ambiguous.
You have to "prefix" the attributes with the respective tablename.
For example like:
$query->andFilterWhere([
'firstTable.first_name' => $this->first_name,
]);
$query->andFilterWhere([
'secondTable.first_name' => $this->first_name2,
]);
Hope you understand.
If not:
Post your SearchModel code here.
(and please format it with the code tags!)
Regards
<?php
namespace app\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Person;
/**
* PersonSearch represents the model behind the search form about `app\models\Person`.
*/
class PersonSearch extends Person
{
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id', 'user_device_id', 'gender', 'mobile_no', 'alternate_contact_no', 'parent_id', 'status'], 'integer'],
[['first_name', 'middle_name', 'last_name', 'short_name', 'date_of_birth', 'email', 'address'], '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)
{
$user_id=Yii::$app->user->identity->id;
$user_type=Yii::$app->user->identity->user_type;
$query='';
if($user_type==Self::ADMIN ){
$query = Person::find();
}elseif($user_type==Self::DEALER){
$query=Person::find()->where(['parent_id'=>$user_id]);
}elseif($user_type==Self::CUSTOMER){
$query=Person::find()
->joinWith(['personalUser'])
->andHaving(['user_id'=>$user_id]);
// $order->link('items', $item);
}
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'user_device_id' => $this->user_device_id,
'gender' => $this->gender,
'date_of_birth' => $this->date_of_birth,
'mobile_no' => $this->mobile_no,
'alternate_contact_no' => $this->alternate_contact_no,
'parent_id' => $this->parent_id,
'status' => $this->status,
]);
$query->andFilterWhere(['like', 'first_name', $this->first_name])
->andFilterWhere(['like', 'middle_name', $this->middle_name])
->andFilterWhere(['like', 'last_name', $this->last_name])
->andFilterWhere(['like', 'short_name', $this->short_name])
->andFilterWhere(['like', 'email', $this->email])
->andFilterWhere(['like', 'address', $this->address]);
return $dataProvider;
}
}
thanku i solved this using below code
$query->andFilterWhere([
'id' => $this->id,
'first_name'=>$this->first_name,
'user_device_id' => $this->user_device_id,
'gender' => $this->gender,
'date_of_birth' => $this->date_of_birth,
'mobile_no' => $this->mobile_no,
'alternate_contact_no' => $this->alternate_contact_no,
'parent_id' => $this->parent_id,
'status' => $this->status,
]);
$query->andFilterWhere(['like', 'person.first_name', $this->first_name])
->andFilterWhere(['like', 'middle_name', $this->middle_name])
->andFilterWhere(['like', 'last_name', $this->last_name])
->andFilterWhere(['like', 'short_name', $this->short_name])
->andFilterWhere(['like', 'email', $this->email])
->andFilterWhere(['like', 'address', $this->address]);
return $dataProvider;
i have done like this ,is it right
$query->andFilterWhere(['like', ''.Person::tableName().'.first_name', $this->first_name])
->andFilterWhere(['like', 'middle_name', $this->middle_name])
->andFilterWhere(['like', ''.Person::tableName().'.last_name', $this->last_name])
->andFilterWhere(['like', 'short_name', $this->short_name])
->andFilterWhere(['like', 'email', $this->email])
->andFilterWhere(['like', 'address', $this->address]);
Well, I would say:
When it works it is at least not wrong.
…
Regarding this case my code looks pretty similar.
But since you wrote you have solved it I have not read all the code.
If someone knows better solutions:
feel free to enlighten us.
Regards