CakePHP 2.5.6 - 使用Paginator查询

时间:2014-11-17 17:17:39

标签: cakephp

我试图制作一个有效的查询(尝试过cakephp直接查询),就像这样:

SELECT *,
   MATCH(b.title) AGAINST('web') as 'tscore',
   MATCH(b.description) AGAINST('web') as 'dscore',  
   MATCH(ba.authorName) AGAINST('web') as 'ascore',
   MATCH(bc.category) AGAINST('web') as 'cscore'
FROM books b
   LEFT JOIN books_has_book_author bhba 
   ON b.id = bhba.books_id

   LEFT JOIN book_author ba
   ON bhba.books_id = ba.id 

   LEFT JOIN book_categories bc
   ON b.book_categories_id = bc.id

WHERE 
   MATCH(b.title) AGAINST('web')
   OR MATCH(b.description) AGAINST('web')  
   OR MATCH(ba.authorName) AGAINST('web')
   OR MATCH(bc.category) AGAINST('web')
ORDER BY
   ('tscore' + 'dscore' + 'ascore' + 'cscore') DESC"

我已经阅读了很多内容,并且我试图用cakephp Paginator实现这一点。

我已经尝试了很多选项/条件,但我想我错过了一些东西......

我已经做了什么:

我的控制器

$searchTerm = filter_var($this->request->query['searchTerm'], FILTER_SANITIZE_STRING);

# Make query
$data = $this->Search->searchTermReturned($searchTerm);


## Paginator don't work this way!!
$this->Paginator->settings = $this->paginate;
$paginatedData = $this->Paginator->paginate($data);
$this->set('data', $paginatedData);
#######  


 ## Pagintor Work this way!!
 $this->Paginator->settings = $this->paginate;
 $paginatedData = $this->Paginator->paginate('Search'); // My model
 $this->set('data', $paginatedData); // Just sent it to view
 #######  

我的模特

class Search extends AppModel {

public $name = 'books';

public function searchTermReturned($searchTerm) 
{

    return $this->query('SELECT *,
        MATCH(b.title) AGAINST("'.$searchTerm.'") as \'tscore\',
        MATCH(b.description) AGAINST("'.$searchTerm.'") as \'dscore\',  
        MATCH(ba.authorName) AGAINST("'.$searchTerm.'") as \'ascore\',
        MATCH(bc.category) AGAINST("'.$searchTerm.'") as \'cscore\'
    FROM books b

    LEFT JOIN books_has_book_author bhba 
        ON b.id = bhba.books_id

    LEFT JOIN book_author ba
        ON bhba.books_id = ba.id 

    LEFT JOIN book_categories bc
        ON b.book_categories_id = bc.id

    WHERE 
        MATCH(b.title) AGAINST("'.$searchTerm.'")
        OR MATCH(b.description) AGAINST("'.$searchTerm.'")  
        OR MATCH(ba.authorName) AGAINST("'.$searchTerm.'")
        OR MATCH(bc.category) AGAINST("'.$searchTerm.'")
    ORDER BY
        (\'tscore\' + \'dscore\' + \'ascore\' + \'cscore\') DESC'); 
}
}

我已尝试在分页功能上设置选项和条件,但可以正确的方式设置所有参数。

我想做的是:

$paginatedData = $this->Paginator->paginate($query);

如何通过cakePHP的核心功能实现这一目标?

提前谢谢大家。

祝你好运, 马塞洛

1 个答案:

答案 0 :(得分:0)

好吧,

如果我可以帮助遇到同样问题的人,我的解决方案是

# Searching query
    $this->Paginator->settings = array(
        'limit' => 5,
        'fields' => array('*', 'MATCH(b.title) AGAINST("'.$searchTerm.'") as \'tscore\',
                            MATCH(b.description) AGAINST("'.$searchTerm.'") as \'dscore\',  
                            MATCH(ba.authorName) AGAINST("'.$searchTerm.'") as \'ascore\',
                            MATCH(bc.category) AGAINST("'.$searchTerm.'") as \'cscore\''),
        'conditions' =>  'MATCH(b.title) AGAINST("'.$searchTerm.'")
                            OR MATCH(b.description) AGAINST("'.$searchTerm.'")  
                            OR MATCH(ba.authorName) AGAINST("'.$searchTerm.'")
                            OR MATCH(bc.category) AGAINST("'.$searchTerm.'")',
        'joins' => array(
                            array(
                                'table' => 'books_has_book_author',
                                'alias' => 'bhba',
                                'type' => 'LEFT',
                                'conditions' => array(
                                    'b.id = bhba.books_id'
                                )
                            ),
                            array(
                                'table' => 'book_author',
                                'alias' => 'ba',
                                'type' => 'LEFT',
                                'conditions' => array(
                                    'bhba.books_id = ba.id'
                                )
                            ),
                            array(
                                'table' => 'book_categories',
                                'alias' => 'bc',
                                'type' => 'LEFT',
                                'conditions' => array(
                                    'b.book_categories_id = bc.id'
                                )
                            )
                        ),
        'order' => '(`tscore` + `dscore` + `ascore` + `cscore`) DESC',
    );


    $this->set('data', $this->Paginator->paginate());

全部谢谢!