如何使用'或' belongsToMany的条件,belongsTo表

时间:2016-06-20 05:25:18

标签: cakephp cakephp-3.0

我不知道如何使用'或'来获取数据?在一个属于自己的表上的条件。我有课程表,导师表和学生表。我为firstname传递一个变量。例如,如果我传递名称' fred'然后我希望所有学生和导师的名字都带有名字' fred'。 我尝试了一个匹配的子句,但这减少了主模型的数据,因此无法正确。我使用了一个orwhere但是没有用,因为我相信我不能将它用于属于这种关系。请问我该怎么做

查询找不到此列: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

1 个答案:

答案 0 :(得分:0)

我通过删除学生的属于这种关系找到了答案,并将其改为了一个人。这是一个相当大的变化,但这种属于自己的关系有时令人头疼。

 $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 , 
                    'OR' => [[ 'Tutors.first_name like' => '%'.$firstname.'%',  'Tutors.last_name like' => '%'.$lastname.'%'],
                             ['Students.first_name like' =>  '%'.$firstname.'%', 'Students.last_name like' => '%'.$lastname.'%']]
                 ])
              ->order(['Lessons.lesson_date' => 'ASC'])   
              ->hydrate(true);