0
我不知道如何獲得一個'或'conditon數據在一個belongsstomany表。我有一張課桌,導師桌和一張學生桌。我爲firstname傳遞一個變量。例如,如果我通過'fred'這個名字,那麼我想讓所有的學生和導師的名字都是'fred'。 我嘗試了一個匹配子句,但是減少了來自主模型的數據,因此無法正確。我使用的地方,但沒有工作,因爲我相信我不能用於這種屬於自己的關係。如何做到這一點請如何使用查找與'或'條件爲belongsToMany,belongsTo表
查詢得到了這列未找到:1054未知列「Students.first_name」在「where子句」
///lesson model
$this->belongsTo('Tutors', [
'foreignKey' => 'tutor_id',
'joinType' => 'LEFT'
]);
$this->belongsToMany('Students', [
'foreignKey' => 'lesson_id',
'targetForeignKey' => 'student_id',
'joinTable' => 'lessons_students'
]);
$query3 = $this->find()
->contain(['Tutors','Subjects', 'TutoringTypes','Terms','Students'])
->select(['Lessons.id','Lessons.lesson_date','Tutors.id','Tutors.first_name','Tutors.last_name',
// 'Students.id','Students.first_name','Students.last_name',
'Subjects.name','TutoringTypes.value'])
->where(['Lessons.lesson_date >' => $a3 , 'Tutors.first_name like' => '%'.$firstname.'%',
'Tutors.last_name like' => '%'.$lastname.'%' ])
->orWhere(['Students.first_name like' => '%'.$firstname.'%' ])
->order(['Lessons.lesson_date' => 'ASC'])
->hydrate(true);
/*
$query3->matching('Students', function ($q) use ($firstname,$lastname) {
return $q
// ->select(['Students.id','Students.first_name','Students.last_name'])
->where(['Students.first_name like' =>'%'.$firstname.'%','Students.last_name like' =>'%'.$lastname.'%', ]);
});
*/
return $query3;
SELECT
Lessons.id AS `Lessons__id`,
Lessons.lesson_date AS `Lessons__lesson_date`,
Tutors.id AS `Tutors__id`,
Tutors.first_name AS `Tutors__first_name`,
Tutors.last_name AS `Tutors__last_name`,
Subjects.name AS `Subjects__name`,
TutoringTypes.value AS `TutoringTypes__value`
FROM
lessons Lessons
LEFT JOIN tutors Tutors ON Tutors.id = (Lessons.tutor_id)
LEFT JOIN subjects Subjects ON Subjects.id = (Lessons.subject_id)
LEFT JOIN tutoring_types TutoringTypes ON TutoringTypes.id = (Lessons.tutoring_type_id)
LEFT JOIN terms Terms ON Terms.id = (Lessons.term_id)
WHERE
(
Students.first_name like '%fred%'
OR (
Lessons.lesson_date > '2016-01-28'
AND Tutors.first_name like '%fred%'
AND Tutors.last_name like '%%'
)
)
ORDER BY
Lessons.lesson_date ASC
LIMIT
20 OFFSET 0
http://book.cakephp.org/3.0/en/orm/query-builder.html
//generated query from the below answer
SELECT
Lessons.id AS `Lessons__id`,
Lessons.lesson_date AS `Lessons__lesson_date`,
Tutors.id AS `Tutors__id`,
Tutors.first_name AS `Tutors__first_name`,
Tutors.last_name AS `Tutors__last_name`,
Subjects.name AS `Subjects__name`,
TutoringTypes.value AS `TutoringTypes__value`
FROM
lessons Lessons
LEFT JOIN tutors Tutors ON Tutors.id = (Lessons.tutor_id)
LEFT JOIN subjects Subjects ON Subjects.id = (Lessons.subject_id)
LEFT JOIN tutoring_types TutoringTypes ON TutoringTypes.id = (Lessons.tutoring_type_id)
LEFT JOIN terms Terms ON Terms.id = (Lessons.term_id)
WHERE
(
Lessons.lesson_date > '2016-01-28'
AND (
Students.first_name like '%%'
OR Tutors.first_name like '%%'
OR Tutors.last_name like '%%'
)
)
ORDER BY
Lessons.lesson_date ASC
LIMIT
20 OFFSET 0
從提供給你所需數據的sql開始。問題標題似乎與您的實際問題很少有關。 – AD7six
由於belongsToMany關係,「匹配」查詢應該是這裏的方法。你有什麼問題?您可能希望查看'autoFields'以在結果集中包含其他數據。 –
我試過匹配的查詢,但是減少了行數。我需要兩個表中名稱相同的行。在原來的文章中有一個匹配的條款註釋掉了。如果還有其他方法,請讓我看看,因爲我不知道該怎麼辦 – ajt