在 cakephp 2.x 中如何使用连接表获取第三个表数据?

Posted

技术标签:

【中文标题】在 cakephp 2.x 中如何使用连接表获取第三个表数据?【英文标题】:In cakephp 2.x How to get third table data using join table? 【发布时间】:2016-11-07 05:40:44 【问题描述】:
'books
    id,name
categories
    id,name
books_categories
    id,book_id,category_id'

' App::uses('AppModel', 'Model'); 类书扩展 AppModel

    public $useTable = 'books';
    public $belongsTo = array(

        'Agerange' => array(
            'className' => 'Agerange',
            'foreignKey' => 'agerange_id'
        ),
        'Publication' => array(
            'className' => 'Publication',
            'foreignKey' => 'publication_id'
        ),
    );

    public $hasMany = array(
        'Category' => array(
            'className' => 'Book_Category',
            'foreignKey' => 'book_id',
            'joinTable' => 'books_categories',
            'fields' => array('Category_id',),          
        )
    );


<?php
App::uses('AppModel', 'Model');
class Book_Category extends AppModel 
    public $useTable = 'books_categories';

    public $belongsTo = array(
        'Book' => array(
            'className' => 'Book',
            'foreignKey' => 'book_id'
        ),
        'Category' => array(
            'className' => 'Category',
            'foreignKey' => 'category_id'
        )
    );




in BooksController ......

$this->paginate = array(
            'contains'=>array('Book','Publication','Agerange','Book_Category'),            
            'order' => 'Book.id ASC');

            $books = $this->paginate('Book');

    enter code here



Book: id: "111", title: "ABC", subtitle: "English Today Primer", author: "Pruthviraj",…,…
Agerange:id: "31", from: "0", to: "2", status: true
Book:id: "111", title: "ABC", subtitle: "English Today Primer", author: "Pruthviraj",…
Category:[Category_id: "5", book_id: "111"]
'

输出现在我只得到 category_id:5 没关系,但我也想要类别的名称字段,所以如何使用 Book 和 Book_Category 获取类别的名称字段?

【问题讨论】:

在对表执行查询之前尝试添加 $this->Model->recursive = 2。它会根据您在模型上设置的 belongsTo 或 w/e 条件自动获取相应的数据 【参考方案1】:

通过将Categoy 添加到Book_Category 来修改您的包含数组

$this->paginate = array(
    'contains' => array(
        'Book',
        'Publication',
        'Agerange',
        'Book_Category' => 'Category'
    ),
    'order' => 'Book.id ASC'
);

或者使用 hasAndBelongsToMany 关联,更简单

首先删除Book_Category

那就换本书的模型

class Book extends AppModel 

public $useTable = 'books';

public $belongsTo = array(
    'Agerange' => array(
        'className' => 'Agerange',
        'foreignKey' => 'agerange_id'
    ),
    'Publication' => array(
        'className' => 'Publication',
        'foreignKey' => 'publication_id'
    ),
);

public $hasAndBelongsToMany = array(
    'Category' => array(
        'className' => 'Category',
        'joinTable' => 'books_categories',
        'foreignKey' => 'book_id',
        'associationForeignKey' => 'category_id'
    )
);

然后,在您的控制器中更改分页设置,如下所示

$this->paginate = array(
    'contains' => array(
        'Publication',
        'Agerange',
        'Category'  
    ),
    'order' => 'Book.id ASC'
);

@link Cakephp HABTM association

【讨论】:

以上是关于在 cakephp 2.x 中如何使用连接表获取第三个表数据?的主要内容,如果未能解决你的问题,请参考以下文章

CakePHP中如何获取表的字段名

如何将数据写入 CakePHP 中 HABTM 连接表的 HABTM 关联?

如何在 CakePHP 2 中为 3 个树状连接表保存数据

CakePHP:如何使用内部联接从两个表中检索数据?

如何使用递归模型创建 cakephp 表单

如何从 CakePHP 中的 URL 字符串获取连接的路由?