【问题标题】:cakephp paginate with associated modelcakephp 与关联模型分页
【发布时间】:2014-07-10 19:13:02
【问题描述】:

用户有很多 User_Questionaries。我想要对有特定问卷的用户进行分页。我使用了以下分页。

$paginate = array(
    'conditions' => array(
        'User.role' => IWOA,
        'UserQuestionary.questionary_id' => $id
    ),
    'recursive' => 1,
    'limit' => 10,
    'order' => array(
        'name' => 'asc'
    ),
    'contain' => array('UserQuestionary')
);

但它不是创建连接查询。它显示Unknown column UserQuestionary.questionary_id' in 'where clause'

有什么问题?我该怎么做?

【问题讨论】:

  • 'contain' 不是 'join',你检查过文档的join section 吗?
  • 您是否在模型中设置了连接? Paginate 用于控制器中,因此如果该控制器的模型没有连接,它将无法工作。我还认为您的 User.role 在您要求它查找字符串时应该在其周围加上引号。

标签: cakephp pagination


【解决方案1】:

最后我使用了连接查询来做到这一点。

$paginate = array(
            'conditions' => array(
                'Iwoa.role' => IWOA,                
            ),
            'joins' => array(
                array(
                    'alias' => 'UserQuestionary',
                    'table' => 'user_questionaries',
                    'type' => 'LEFT',
                    'conditions' => 'UserQuestionary.user_id = Iwoa.id AND UserQuestionary.questionary_id = ' . $id
                )
            ),
            'limit' => 10,
            'order' => array(
                'name' => 'asc'
            ),
        );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多