2016-07-27 2 views
0

제품 테이블과 제품 라인 테이블이 있습니다. 제품은 제품 라인과 관계가 있습니다. 단일 검색 상자를 사용하여 product 테이블의 필드와 productlines 테이블의 필드를 검색하고 싶습니다.Yii2 하나의 검색 상자가 두 개의 다른 표에서 필드를 검색합니다.

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'product_id' in where clause is ambiguous 
The SQL being executed was: SELECT COUNT(*) FROM `sim_product` LEFT JOIN `sim_productlines` ON `sim_product`.`product_id` = `sim_productlines`.`product_id` WHERE ((`product_id` IN ('2', '3')) OR (`product_catalog` LIKE '%A%')) OR (`internal_code` LIKE '%A%') 

내가 잘못 가고 어디 사람이 나에게 도움을 줄 수 어떤 가능한 해결책이 될 수 있습니다 : 나는이 작업을 수행 할 때 내 상품 검색 모델

public function search($params) 
    { 
     $query = Product::find()->where(['product_id' => $this->getProductID()]); 

     // add conditions that should always apply here 
     $query->joinWith('productlines'); 

     $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([ 
      'product_id' => $this->product_id, 
      'product_region' => $this->product_region, 
      'product_created' => $this->product_created, 
      'product_lastchecked' => $this->product_lastchecked, 
      'sdsref_id' => $this->sdsref_id, 

     ]); 

     // var_dump($this->getProductID()); exit(); 
     $query->andFilterWhere(['like', 'product_name', $this->product_name]) 
      ->andFilterWhere(['like','product_id', $this->product_id]) 
      ->orFilterWhere(['like', 'product_catalog', $this->code]) 
      ->andFilterWhere(['=', 'product_aka', $this->product_aka]) 
      ->orFilterWhere(['like', 'internal_code' , $this->code]); 

     return $dataProvider; 
    } 

가 나는 오류가 발생합니다.

감사

+0

접두사 테이블 이름을'참조 WHERE ((tableName.product_id'.) 두 테이블에 같은 이름이 있기 때문에 MySQL에서 열 이름을 확인할 수 없습니다. –

+0

테이블 이름을 추가하면 unde 벌금이 부과 된 열 –

+0

은 queryBuilder를 사용합니다. –

답변

0

Yii2 당신이 내 열 이름을 접두사 수 있습니다 ActiveQuery 같은 :

$query->andFilterWhere(['like', 'product.product_name', $this->product_name]) 
    ->andFilterWhere(['like','productline.product_id', $this->product_id]) 
    ->orFilterWhere(['like', 'productline.product_catalog', $this->code]) 
    ->andFilterWhere(['=', 'product.product_aka', $this->product_aka]) 
    ->orFilterWhere(['like', 'product.internal_code' , $this->code]); 

출처 : http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#relational-data

하는 부분 "관계로 결합"

관련 문제