CakePHP:对相关 hasMany 字段进行查询约束的分页?

发布于 2024-12-05 17:17:51 字数 2026 浏览 1 评论 0原文

类似于 这个问题 - 但由于我对 CakePHP 的无能,我似乎无法弄清楚这一点。这是我的设置。

我有一个模型名称 User,它有许多 Comments。我想要一份包含至少一条评论的所有不可见用户的列表。我们的控制器设置了将 records 设置为 $this->paginate() 的方法,无需传递任何值;相反,$paginate var 是用条件和类似的东西设置的。

class UsersController extends AppController {
    var $name = 'Users';
    var $paginate;

    function new_users() {
        $this->User->recursive = 1;
        $this->paginate['User']['contain'][] = 'Comment';
        $this->paginate['User']['order'] = 'User.DateCreated DESC';

        $conditions = array();
        // Get invisible users...
        $conditions[] = array('User.Status = ?' => 'Invisible');
        $conditions[] = array('User.Enabled' => true);
        // ...with at least one active comment
        $conditions[] = array('Comment.Status' => 'Active');

        // Load these conditions and fire pagination to render the view
        $this->paginate['User']['conditions'] = $conditions;
        $this->set('records', $this->paginate());
    }
} 

我对 CakePHP 还很陌生,很多东西仍然在我的脑海中。我只想要一个具有活跃评论用户列表。现在,这个查询为我带来了 0 个用户。如果我取出有关 Comment.Status 的行,查询就会通过,但我会得到所有不可见的 Users,无论他们是否有评论。

我在网上看到 paginate() 调用的所有地方,都已将一些内容传递给它(例如 paginate('User', array('Comment.Status' => 'Active' )),我已经尝试过但没有任何收获)。

编辑:我有以下代码,虽然更干净,但仍然给出了相同的结果。

$this->paginate = array(
  'conditions' => array(
    'User.Status' => 'Invisible',
    'User.Enabled' => true,
  ),
  'contain' => array(
    'Comment' => array(
      'conditions' => array(
        'Comment.Status' => 'Active',
      )
    )
  ),
  'recursive' => 1,
  'order' => 'User.DateCreated DESC',
);
$data = $this->paginate('User');

Similar to this question — but due to my ineptitude with CakePHP, I can't seem to figure this out. Here is my setup.

I've got a model name User which has many Comments. I want a list of all invisible Users with at least one Comment. Our controller has methods set up to set records as $this->paginate() without any values passed to it; instead, the $paginate var is set up with conditions and things like that.

class UsersController extends AppController {
    var $name = 'Users';
    var $paginate;

    function new_users() {
        $this->User->recursive = 1;
        $this->paginate['User']['contain'][] = 'Comment';
        $this->paginate['User']['order'] = 'User.DateCreated DESC';

        $conditions = array();
        // Get invisible users...
        $conditions[] = array('User.Status = ?' => 'Invisible');
        $conditions[] = array('User.Enabled' => true);
        // ...with at least one active comment
        $conditions[] = array('Comment.Status' => 'Active');

        // Load these conditions and fire pagination to render the view
        $this->paginate['User']['conditions'] = $conditions;
        $this->set('records', $this->paginate());
    }
} 

I am so new to CakePHP that a lot of this stuff is still over my head. I just want a list of Users that have active Comments. Right now, this query nets me 0 users. If I take out the line regarding Comment.Status, the query goes through, but I get all invisible Users regardless of if they have comments.

Everywhere online that I've seen the paginate() call, things have been passed to it (like paginate('User', array('Comment.Status' => 'Active')), which I've tried and nets me nothing).

Edit: I've got the following code, while cleaner, still gives me the same results.

$this->paginate = array(
  'conditions' => array(
    'User.Status' => 'Invisible',
    'User.Enabled' => true,
  ),
  'contain' => array(
    'Comment' => array(
      'conditions' => array(
        'Comment.Status' => 'Active',
      )
    )
  ),
  'recursive' => 1,
  'order' => 'User.DateCreated DESC',
);
$data = $this->paginate('User');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是我的挚爱i 2024-12-12 17:17:51

我通常使用即时分页更改呵呵。这是一些示例代码。

$this->paginate = array(
    'conditions' => array('Recipe.title LIKE' => 'a%'),
    'limit' => 10    );
$data = $this->paginate('Recipe');

在这里您可以阅读有关分页控制器设置的更多信息。但是,如果您在模型中已经有了关联(hasMany 等),则如果您将递归设置为 1,则数据会自动获取,而不需要包含(您仍然可以使用它来仅获取您想要的数据)。一个关于它如何寻找您的示例:

$this->paginate = array(
    'conditions' => array(
         'User.Status' => 'Invisible',
         'User.Enabled' => true
    ),
    'contain' => array('Comment' => array(
           'conditions' => array('Comment.Status' => 'Active')
    )),
    'recursive' => 1,
    'order' => 'User.DateCreated DESC'
);
$data = $this->paginate('User');

这就是您在帖子中放置的内容。如果你粘贴你想要的sql查询,我也许可以帮助你更多。另外,尝试检查 sql 输出,以便了解条件是否有问题。如果您没有也不知道如何放置 sql 输出,请执行此操作。

var_dump($this->User->getQuery(
    'conditions' => array(
         'User.Status' => 'Invisible',
         'User.Enabled' => true,
         'Comment.Status' => 'Active'
    ),
    'contain' => 'Comment',
    'order' => 'User.DateCreated DESC'
));

希望对你有帮助,如果没有,请给我留言。

编辑:这是你需要做的才能得到你想要的东西。 :D 更改表格和条件以满足您的需要。

$this->paginate = array(
    'conditions' => array(
         'User.Status' => 'Invisible',
         'User.Enabled' => true
    ),
    'joins' =>  array(
        array(
          'table' => 'name_of_comment_table',
          'alias' => 'Comment',
          'type' => 'inner',
          'conditions' => array(
              'Comment.user_id = User.id',
              'Comment.Status' => 'Active'
           )
         )
     ),
    'contains' => array('Comment'),
    'group' => 'User.id',
    'recursive' => 1,
    'order' => 'User.DateCreated DESC'
);
$data = $this->paginate('User');

I usually use the paginate changes on-the-fly hehe. Here is some example code.

$this->paginate = array(
    'conditions' => array('Recipe.title LIKE' => 'a%'),
    'limit' => 10    );
$data = $this->paginate('Recipe');

Here you can read more about controller setup for paginations. But if you already have the associations in the model (hasMany, etc) the data is auto-fetched if you put recursive to 1 without the need of contain (you may still use it to get only the data you want). an example of how it will look for you:

$this->paginate = array(
    'conditions' => array(
         'User.Status' => 'Invisible',
         'User.Enabled' => true
    ),
    'contain' => array('Comment' => array(
           'conditions' => array('Comment.Status' => 'Active')
    )),
    'recursive' => 1,
    'order' => 'User.DateCreated DESC'
);
$data = $this->paginate('User');

This is what you put in your post. If you paste the sql query of what you want, I may be able to help you more. Also, try checking the sql output so you know if something is wrong in the conditions. if you don't have and don't know how to put the sql output just do this.

var_dump($this->User->getQuery(
    'conditions' => array(
         'User.Status' => 'Invisible',
         'User.Enabled' => true,
         'Comment.Status' => 'Active'
    ),
    'contain' => 'Comment',
    'order' => 'User.DateCreated DESC'
));

Hope it helps you, if not, leave me a comment.

EDIT: This is what you need to do to get what you want. :D Change the table and the condition to fit your needs.

$this->paginate = array(
    'conditions' => array(
         'User.Status' => 'Invisible',
         'User.Enabled' => true
    ),
    'joins' =>  array(
        array(
          'table' => 'name_of_comment_table',
          'alias' => 'Comment',
          'type' => 'inner',
          'conditions' => array(
              'Comment.user_id = User.id',
              'Comment.Status' => 'Active'
           )
         )
     ),
    'contains' => array('Comment'),
    'group' => 'User.id',
    'recursive' => 1,
    'order' => 'User.DateCreated DESC'
);
$data = $this->paginate('User');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文