【问题标题】:How to get the data from three tables with join in cakephp?如何通过加入 cakephp 从三个表中获取数据?
【发布时间】:2015-02-17 07:02:20
【问题描述】:

如何通过cakephp中的join从三个表中获取公共包id的数据?

包ID在三个表之间是通用的。

我想从三个具有特定包 ID 的表中获取数据。

是否有任何查询可以同时从三个表中获取特定包 ID 的数据?

【问题讨论】:

  • 用它们的模式/表字段指定你的表名
  • OP,要检查答案吗??

标签: php mysql cakephp join


【解决方案1】:

有两种主要方法可以做到这一点。其中一种是标准的CakePHP方式,另一种是使用自定义join

CakePHP Join Multiple Table

【讨论】:

    【解决方案2】:

    连接可以如下完成,用你的表代替我的

    假设这里我的表,控制器是CourseLessonsReferenceController,所以我可以加入其他2个或多个表如下:

    $conditionForId = array('CourseLessonsReference.course_id'=>$package_id,
        );  
    
    
        //Pagination logic
        $this->paginate = $allConditionsFields = array('conditions' => $conditionSearchLessonsByCourse,                                                                                                 
                                "joins" => array(
                                                array(//UserCourse = Course Join
                                                    "table" => "courses",
                                                    "alias" => "Course1",
                                                    "type" => "INNER",
                                                    "conditions" => array(
                                                        "Course1.id = CourseLessonsReference.course_id"
                                                    )
                                                ),//For Category = Course Join
                                                array(
                                                    "table" => "course_categories",
                                                    "alias" => "CourseCategory",
                                                    "type" => "INNER",
                                                    "conditions" => array(
                                                        "CourseCategory.id = Course1.course_category_id"
                                                    )
                                                )
                                            ),
                                'recursive' =>  2                               
                                );
    
    
     $resultOfJoin = $this->CourseLessonsReference->find('all', $allConditionsFields);
    

    $resultOfJoin 将为您提供 3 个表的连接结果。

    表示将当前表CourseLessonsReference与$allConditionsFields数组中Join标签中指定的表连接起来。

    由于您没有提供表的架构,因此在我的情况下,当前控制器/模型/表是 CourseLessonsReference,它将有参考。参考与课程相关联,因此 course_id 是 course_lessons_reference 表中的外键。所以这是1个加入。现在 Course 已连接到 Category 所以,课程将有 course_category_id 作为外键。

    【讨论】:

      猜你喜欢
      • 2021-01-03
      • 1970-01-01
      • 1970-01-01
      • 2021-08-05
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多