我这里是OrderContract.php 和OrderContractSearch.php
首先在OrderContract.php里定义要关联的表
public function getCustomerMsg(){
return $this->hasOne(CustomerMsg::className(), ['id'=>'customer_id']);
}
注意这里的getCustomerMsg 命名会影响到search里的连表名称,比如我这里改为getCustomerMsgTest,那么search里joinwith表名也要改为
然后在OrderContractSearch.php里customerMsgTest, (c要小写!!!)
$query = OrderContract::find()->alias('oc');
$query->joinWith(['customerMsg']);
然后就是表别名,似乎只能给一个表加别名,同时给2个表加别名会报错。
至于查询语句记录一下 OR和AND同时存在的用法
$query->andFilterWhere([
'shop_id' => $this->shop_id,
'status' => $this->status,
'customer_charge'=>$this->customer_charge,
]);
$query->andFilterWhere(['OR',
['like', 'oc.mobile', $this->mobile],
['like', 'oc.address', $this->mobile],
['like', 't_customer_msg.customer_name', $this->mobile]
]);
$query->andFilterWhere(['like', 'name', $this->name])
->andFilterWhere(['like', 'code', $this->code])
->andFilterWhere(['like', 'remarks', $this->remarks]);