Cakephp3:如何在包含条件的分页中搜索数据?

Cakephp3: How to search data in paginate with contain condition?

我有两个table名字分别是杂和用户。在杂项中table有driver_id和vendor_id。用户 table 中的两个 ID 都带有名称 ID 列。这是我的示例代码。

src/Model/Table/Miscellaneous.php

<?php

namespace App\Model\Table;

use Cake\ORM\Table;
use Cake\Validation\Validator;

    class MiscellaneousTable extends Table {

        public function initialize(array $config) {
            $this->table('miscellaneous');
            $this->displayField('name');
            $this->primaryKey('id');

            $this->addBehavior('Timestamp');
            $this->belongsTo('Vendors', [
                'foreignKey' => 'vendor_id',
                'className' => 'users',
                'joinType' => 'left'
            ]);
        $this->belongsTo('Drivers', [
                'foreignKey' => 'driver_id',
                'className' => 'users',
                'joinType' => 'left'
            ]);
        }
    }

我想在供应商名称和驱动程序名称中搜索。这是我的分页条件:

if (!empty($this->request->data['vendor']['name'])) {
   $vendor_name = $this->request->data['vendor']['name'];
}
if (!empty($this->request->data['driver']['name'])) {
    $driver_name = $this->request->data['driver']['name'];
}
$miscellaneous = $this->paginate($this->Miscellaneous, ['contain' => ['Drivers' => function(\Cake\ORM\Query $q) {
    return $q->where(['Drivers.name LIKE ' => '%'.$driver_name.'%']);
    }, 'Vendors' => function(\Cake\ORM\Query $q) {
    return $q->where(['Vendors.name LIKE ' => '%'.$vendor_name.'%']);
    }], 'conditions' => $con, 'order' => ['Miscellaneous.id' => 'DESC']]);

但它什么也没显示。请帮助我找到正确的解决方案。 它在没有供应商和驱动程序名称的情况下工作。

对于 belongTo 关联(Cake 可以使用普通连接处理)我经常使用以下代码片段:

<?PHP
  $whereArray = [];
  if (!empty($this->request->data['vendor']['name'])) {
    $whereArray[] = ['Vendors.name LIKE ' => '%'.$this->request->data['vendor']['name'].'%'];
  }
  if (!empty($this->request->data['driver']['name'])) {
    $whereArray[] = ['Drivers.name LIKE ' => '%'.$this->request->data['driver']['name'].'%'];
  }
  $miscellaneous = $this->paginate($this->Miscellaneous->find('all', ['contain' => ['Vendors', 'Drivers']])->where($whereArray)->order(['Miscellaneous.id' => 'DESC']);

?>

注解:我经常使用"all"作为查找器,因为我还需要列表中的完整记录...