排除HABTM中的某些字段find()

时间:2012-02-09 17:29:50

标签: cakephp-1.3

我正在使用CakeDC标签插件,我想在我的图书模型中找到标签。

在我的Books控制器中,我有一个index()方法,它返回分页结果。目前,我的index()方法看起来像这样。

function index() {
    $this-Book>recursive = 0;
    $this->paginate = array(
        'fields' => array('Book.id','Book.name'),
        'conditions' => array('Book.status' => 1),
        'order' => array('Book.id' => 'DESC'),
        'limit' => 10
    );
    $books = $this->paginate($this->Book);
    $this->set('books', $books);
}

当我进行调试时,这些是我的结果: -

Array
(
    [0] => Array
        (
            [Book] => Array
                (
                    [id] => 12
                    [name] => A randomly random book
                    [tags] => 
                )

        )
)

如果我将递归提升为1而不是0,则将填充tags数组,调试输出如下: -

Array
(
    [0] => Array
        (
            [Book] => Array
                (
                    [id] => 12
                    [name] => A randomly random book
                    [tags] => wicked
                )
            [Tag] => Array
                (
                    [0] => Array
                        (
                            [id] => 4f30d76a-20dc-44ce-943c-048c98b7a787
                            [identifier] => 
                            [name] => wicked
                            [keyname] => wicked
                            [weight] => 0
                            [created] => 2012-02-07 18:48:58
                            [modified] => 2012-02-07 18:48:58
                            [Tagged] => Array
                                (
                                    [id] => 4f30d76a-f55c-4ab7-b076-048c98b7a787
                                    [foreign_key] => 53
                                    [tag_id] => 4f30d76a-20dc-44ce-943c-048c98b7a787
                                    [model] => Book
                                    [language] => en-us
                                    [created] => 2012-02-07 18:48:58
                                    [modified] => 2012-02-07 18:48:58
                                )

                        )
                )

        )
)

提高递归性能很好地完成了工作。但是,CakePHP返回了包括创建和修改日期字段在内的所有内容。无论如何让CakePHP只填充$ book ['Book'] ['tags']数组而不是[Tag]和[Tagged]数组?感谢

1 个答案:

答案 0 :(得分:2)

我不知道这个特定的插件,但通常的方法是对数据库中检索到的数据进行更细粒度的控制,并采用Containable行为:http://book.cakephp.org/1.3/en/view/1323/Containable

将其添加到Book模型后,您只需检索Tag s的名称和ID:

$this-Book>recursive = -1;
$this->paginate = array(
    'fields' => array('Book.id','Book.name'),
    'contain' => array(
        'Tag'=>array('id','name'),
    ),
    'conditions' => array('Book.status' => 1),
    'order' => array('Book.id' => 'DESC'),
    'limit' => 10
);