【问题标题】:Cakephp paginate displaying multiples of some recordsCakephp 分页显示某些记录的倍数
【发布时间】:2011-06-12 06:00:00
【问题描述】:

这是我第一次使用 CakePHP 编程。我正在使用 cakePHP 1.3.3。我遇到了这个问题,我似乎无法弄清楚问题是什么。我的功能如下:

function view_members(){
  $data = $this->paginate('User',array('User.level <=' => '10'));
  print_r($data);
}

在我的控制器开始时,分页设置为:

var $paginate = array(
'limit' => 20,
'order' => array('User.id' => 'asc'),
'User' => array(
  'fields' => array('User.id','User.level','User.name','User.status'),
  'recursive' => 0
 )
);

在我的模型中,用户是这样设置的:

    class User extends AppModel {
    var $name = 'User';
    var $hasOne = array(
        'Users_detail' => array(
            'className' => 'Users_detail',
            'dependent' => true,
        ),
        'Users_calendar' => array(
            'className' => 'Users_calendar',
            'dependent' => true,
        ),
    );
    var $hasMany = array(
        'Users_transaction' => array(
            'className' => 'Users_transaction',
            'order' => 'Users_transaction.id ASC', 
            'dependent' => true,
        ),
        'Users_notice' => array(
            'className' => 'Users_notice',
            'order' => 'Users_notice.id DESC',
            'dependent' => true,
        ),
    );
}

我的问题是当我访问该函数时,我得到了 1 条记录的多个实例。

这是我得到的数组:

Array
(
    [0] => Array
        (
            [User] => Array
                (
                    [id] => 3
                    [level] => 5
                    [name] => Matthew
                    [status] => 1
                )

        )

    [1] => Array
        (
            [User] => Array
                (
                    [id] => 3
                    [level] => 5
                    [name] => Matthew
                    [status] => 1
                )

        )

    [2] => Array
        (
            [User] => Array
                (
                    [id] => 3
                    [level] => 5
                    [name] => Matthew
                    [status] => 1
                )

        )

    [3] => Array
        (
            [User] => Array
                (
                    [id] => 3
                    [level] => 5
                    [name] => Matthew
                    [status] => 1
                )

        )

    [4] => Array
        (
            [User] => Array
                (
                    [id] => 4
                    [level] => 5
                    [name] => Larry
                    [status] => 1
                )

        )

    [5] => Array
        (
            [User] => Array
                (
                    [id] => 5
                    [level] => 5
                    [name] => Brian
                    [status] => 1
                )

        )
);

在此示例中,尽管 MySQL 数据库中只有 1 条记录,但 Matthew 在 Array 中出现了 4 次。我一直试图弄清楚它为什么会这样做,但到目前为止,我还没有成功。有没有人经历过这种情况?

【问题讨论】:

  • 在您的配置中将调试设置为 2。调用 paginate() 生成的 SQL 查询是什么?

标签: php cakephp pagination


【解决方案1】:

没有看到确切的 SQL Cake 正在执行,很难找到确切的答案,但我最好的猜测是它与相关表中存在的数据有关。用户 Matthew 在与 User 表相关的表之一中是否有关联行?

您正在使用recursive = 0,但这可能不会达到您的预期(它实际上确实会连接到与它有belongsTo 关系的其他模型)。如果您不想与其他表进行任何联接,请使用recursive = -1。请参阅 Cake 手册中的 documentation for recursive 以了解其工作原理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 2012-10-05
    相关资源
    最近更新 更多