1

在 cakephp 1.2 中,我有一个包含书籍列表的表格 (biblio),还有一个包含标签列表的表格 (tematiche)。
我有这个代码来获取书籍列表:

if (isset($autore_diviso))
        {
            $dim=count($autore_diviso);
            $i=0;
            while ($i<$dim)
            {
                $conditions[]=array ('autori LIKE ?' => array(
                    '%' . $autore_diviso[$i][0] . '%'));
                $i++;
            }
        }
        else
            $conditions[]=array('autori LIKE' => "%$a%");
        if (!$anno&&!$anno2)    // Da sistemare.
            $conditions=$conditions;
        else {
        if (!$anno)
            $conditions[] = array('anno <=' => "$anno2");          
        if (!$anno2)
            $conditions[] = array('anno >=' => "$anno");
        }
        if ($anno&&$anno2)
            $conditions[] = array('anno BETWEEN ? AND ?' => array($anno,$anno2)); }


现在我还需要获取一本书的标签,如何将其添加到此查询中?

4

1 回答 1

1

尝试Tag model在控制器中绑定

在控制器中

<?php

$this->Book->bindModel(array
(
    'hasMany' => array
    (
        'Tag' => array
        (
            'foreignKey' => false,
            'conditions' => array
            (
                'Book.id = Tag.book_id'
            )
        )
    )
));

$books = $this->Book->find('all',array
(
    'conditions' => array
    (
        'Book.id' => 'SOME_ID'
    )
));

pr($books);
exit;

以上调试结果

Array
(
    [Book] => Array
    (
        [id] => SOMEID
        [name] => BOOKNAME
    )
    [Tag] => Array
    (
        [0] => Array
        (
            [id] => 1
            [name] => TAGNAME
            [book_id] => SOME_ID
        )
        [1] => Array
        (
            [id] => 2
            [name] => TAGNAME
            [book_id] => SOME_ID
        )
        [2] => Array
        (
            [id] => 4
            [name] => TAGNAME
            [book_id] => SOME_ID
        )
    )
)
于 2013-02-13T08:54:21.827 回答