CakePHP 3.6:联接查询

时间:2018-10-09 14:11:26

标签: php join cakephp

我有下表:

customers[id, name, surname, phone, text, balance, created]

service_types[id, title, price, length, is_subscription, created, payment]

customer_service_types[id, customer_id, service_type_id, price, created]

与关系:

ServiceTypesTable.php:

$this->hasMany('CustomerServiceTypes', [
        'foreignKey' => 'service_type_id'
    ]);

CustomerServiceTypesTable.php:

$this->belongsTo('Customers', [
        'foreignKey' => 'customer_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsTo('ServiceTypes', [
        'foreignKey' => 'service_type_id',
        'joinType' => 'INNER'
    ]);

这是我现在的代码:

    $customerServiceTypes = TableRegistry::get('CustomerServiceTypes');
    $customers_count = $customerServiceTypes->find()
        ->select(['customer_id'])
        ->distinct()
        ->where(['service_type_id <>' => 3])->count(); 

我想对其进行修改,以便从

上的whereservice_types.payment = 0的联接中检出customer_service_types,是否签入service_types子句
customer_service_types.service_type_id = service_types.id

0 个答案:

没有答案