我有两个:
Providers
和Purchasetopay
具有以下关联:
$Providers->hasMany('Purchasetopay', [
'foreignKey' => 'providers_id'
]);
并且我还有以下操作:
public function fornecedores(){
$Providers = TableRegistry::get('Providers');
$pesquisar = $this->request->query('pesquisar');
if(!empty($pesquisar)){
$this->paginate = [
'conditions' =>
[ 'OR' =>
['Providers.cnpj' => $pesquisar,
'Providers.corporatename LIKE' => '%'.$pesquisar.'%',
]
]
];
}
$query = $Providers->find()->contain(['Telephones','Purchasetopay'=>['SupplierNotes']]);
$providers = $this->paginate($query);
$this->set(compact('providers'));
}
我需要将Purchasetopay字段添加到OR
条件中。怎么样?
我看到了以下解决方案:https://stackoverflow.com/a/39559315/11239369
但是,我不知道如何使我的代码适应这种方式。
答案 0 :(得分:1)
您可以使用orWhere / andWhere,如下所示:
$query = $Providers->find()
->contain(['Telephones','Purchasetopay'=>['SupplierNotes']])
->where( ['Providers.cnpj' => $pesquisar])
->orWhere(['Providers.corporatename LIKE' => '%'.$pesquisar.'%' ]);