通过分页上的关联数据查找

时间:2019-12-03 14:32:38

标签: cakephp cakephp-3.0 model-associations

我有两个: ProvidersPurchasetopay具有以下关联:

$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 但是,我不知道如何使我的代码适应这种方式。

1 个答案:

答案 0 :(得分:1)

您可以使用orWhere / andWhere,如下所示:

$query = $Providers->find()
          ->contain(['Telephones','Purchasetopay'=>['SupplierNotes']])
          ->where( ['Providers.cnpj' => $pesquisar])
          ->orWhere(['Providers.corporatename LIKE' => '%'.$pesquisar.'%' ]);