使用cakephp模型处理自定义查询中的分页

时间:2012-11-27 11:12:20

标签: pagination cakephp-2.0

我有从subscriber_0到subscriber_9的10个订阅者表,我通过订阅者模型中的自定义查询通过union获取所有数据,在函数getAllSubscribers中,此表执行查询并将结果返回给订阅者控制器,我有设置$ useTable = false因为没有订阅者表,我的问题是如何在控制器中对这些数据进行分页?

作为参考我在这里写我的查询并返回结果 查询:

 $query = 'SELECT * FROM (
            SELECT * FROM subscriber_1
            UNION 
            SELECT * FROM subscriber_2
            UNION 
            SELECT * FROM subscriber_3
            UNION 
            SELECT * FROM subscriber_4
            UNION 
            SELECT * FROM subscriber_5
            UNION 
            SELECT * FROM subscriber_6
            UNION 
            SELECT * FROM subscriber_7
            UNION 
            SELECT * FROM subscriber_8
            UNION 
            SELECT * FROM subscriber_9
            UNION 
            SELECT * FROM subscriber_0
            ) AS subscriber WHERE created > \'' . $startDate . '\' AND created < \'' . $endDate . '\'';

结果:

array(
(int) 0 => array(
    'subscriber' => array(
        'a_party' => '923003210861',
        'subtype' => '0',
        'stat' => '0',
        'created' => '2012-11-26 06:53:31',
        'updated' => null
    )
),
(int) 1 => array(
    'subscriber' => array(
        'a_party' => '923005264511',
        'subtype' => '0',
        'stat' => '0',
        'created' => '2012-11-26 06:53:31',
        'updated' => null
    )
)
,
.
.
.
.(int) 50 => ...

我在订阅者控制器中添加了这个

    public $paginate = array(
    'limit' => 10
);

我还需要添加什么?

我见过这个但没有帮助

CakePHP pass custom query from controller into model paginate()

编辑:添加控制器代码

            $data = $this->Subscriber->getAllSubscribers();//This model method returns custom data from query
        $this->paginate = array('fields' => $selectedField);
        $paginatedData = $this->paginate($data);
        //debug($paginatedData);

        $this->set('subslist', $paginatedData);

以上代码无效,我做错了什么,谢谢

2 个答案:

答案 0 :(得分:8)

以下是我如何实现它: 型号代码

/**
 * Overridden paginate method - group by week, away_team_id and home_team_id
 */
public function paginate($conditions, 
    $fields, 
    $order, 
    $limit, 
    $page = 1, 
    $recursive = null, 
    $extra = array()) {

    $recursive = -1;
    $sql = "SELECT * FROM (
        SELECT * FROM blacklist_0
        UNION
        SELECT * FROM blacklist_1
        UNION 
        SELECT * FROM blacklist_2
        UNION 
        SELECT * FROM blacklist_3
        UNION 
        SELECT * FROM blacklist_4
        UNION 
        SELECT * FROM blacklist_5
        UNION 
        SELECT * FROM blacklist_6
        UNION 
        SELECT * FROM blacklist_7
        UNION 
        SELECT * FROM blacklist_8
        UNION 
        SELECT * FROM blacklist_9
        UNION 
        SELECT * FROM blacklist_0
    ) AS Blacklist LIMIT " . (($page - 1) * $limit) . ', ' . $limit;

    $results = $this->query($sql);
    return $results;
}

/**
 * Overridden paginateCount method
 */
public function paginateCount($conditions = null, 
    $recursive = 0, 
    $extra = array()) {

    $sql = "SELECT * FROM (
        SELECT * FROM blacklist_1
        UNION 
        SELECT * FROM blacklist_2
        UNION 
        SELECT * FROM blacklist_3
        UNION 
        SELECT * FROM blacklist_4
        UNION 
        SELECT * FROM blacklist_5
        UNION 
        SELECT * FROM blacklist_6
        UNION 
        SELECT * FROM blacklist_7
        UNION 
        SELECT * FROM blacklist_8
        UNION 
        SELECT * FROM blacklist_9
        UNION 
        SELECT * FROM blacklist_0
    ) AS Blacklist";

    if ($conditions['Blacklist.b_party'] <> null) {
        $sql = $sql . ' WHERE Blacklist.b_party = \'' . $conditions['Blacklist.b_party'] . '\'';
    } else if ($conditions['Blacklist.a_party'] <> null) {
        $sql = $sql . ' WHERE Blacklist.a_party = \'' . $conditions['Blacklist.a_party'] . '\'';
    }

    $this->recursive = $recursive;
    $results = $this->query($sql);

    return count($results);
}

不要忘记我用过

public $useTable = false;

在模型中,但关键是如何覆盖方法,我最终在我的同事的帮助下想出,控制器代码很简单。这里是: 控制器代码:

public function index() {
    $this->set('msisdn', "");
    $this->Blacklist->recursive = 0;
    $this->set('blacklists', $this->paginate());
}

答案 1 :(得分:0)

您无法在自定义查询中使用标准分页,您必须自己创建。

Custom Query Pagination