在 paginate 上通过关联数据查找

Find by associated data on paginate

我有两个: 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 条件。如何? 我看到了这个解决方案: 但是,我不知道如何调整我的代码以这种方式工作。

您可以使用 orWhere/andWhere 如下:

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