2

我有 10 个订阅者表,从订阅者_0 到订阅者_9,我通过订阅者模型中的自定义查询通过联合从它们获取所有数据,在函数 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 将自定义查询从控制器传递到模型 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);

上面的代码不起作用,我做错了什么,谢谢

4

2 回答 2

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());
}
于 2012-11-28T14:55:30.963 回答
0

您不能在自定义查询上使用标准分页,您必须自己创建。

自定义查询分页

于 2012-11-27T20:49:12.640 回答