ЦЕЛЬ: ОТОБРАЗИТЬ СПИСОК: ИД ЧЛЕНА / ИМЯ ЧЛЕНА / ПОСЕТИМОЙ ДАТЫ / УРОВЕНЬ ЧЛЕНА
Это оператор SQL, который я хочу кодировать в Cake PHP 4:
SELECT visits.member_id, members.mbr_name, visits.created, batches.grade_id
FROM visits
LEFT JOIN members ON members.mbrid = visits.member_id
LEFT JOIN batches ON batches.member_id = members.mbrid
WHERE batches.batch = '2020' AND (batches.sub_batch = '0' OR batches.sub_batch = '1')
ORDER BY visits.created DESC
Как видите, второе предложение LEFT JOIN относится только к членам, а не к посещениям главной таблицы.
Это код, который я кодирую в Cake PHP 4:
$this->Visits->find()
->contain('Members.Batches', function (Query $q) {
return $q
->select('member_id', 'batch', 'sub_batch', 'grade_id')
->where(['Batches.batch' => '2020', 'Batches.sub_batch' => '0']);
})
->contain('Members')
->order(['Visits.created' => 'DESC'])
);
В режиме отладки я вижу два (2) SQL сгенерированных. Не то, что я ожидал:
SELECT
Visits.id AS Visits__id,
Visits.member_id AS Visits__member_id,
Visits.vst_datetime AS Visits__vst_datetime,
Visits.location_id AS Visits__location_id,
Visits.created AS Visits__created,
Visits.modified AS Visits__modified,
Members.id AS Members__id,
Members.mbrid AS Members__mbrid,
Members.mbr_name AS Members__mbr_name,
Members.mbr_type AS Members__mbr_type,
Members.created AS Members__created,
Members.modified AS Members__modified
FROM
visits Visits
LEFT JOIN members Members ON Members.mbrid = (Visits.member_id)
ORDER BY
Visits.created DESC
LIMIT
20 OFFSET 0
SELECT
Batches.member_id AS Batches__member_id
FROM
batches Batches
WHERE
(
Batches.member_id in (
187, 1471, 1470, 1463, 250, 350, 1000,
501, 300, 255, 254, 253, 3, 303, 215,
305, 202, 201
)
AND Batches.batch = '2020'
AND Batches.sub_batch = '0'
)