小编lup*_*upi的帖子

CakePHP3-如何更改分页网址

我的网址已映射,例如: localhost/seo-url/seo-variable

如果未映射,则网址如下所示: localhost/controller-name/index/2

$routes->connect('/seo-url/:product_id',[,'controller' => 'ControllerNames', 'action' => 'index','product_id' => null],['pass' => [product_id'],'product_id' => '[a-z0-9\-_]+']); ------- CakePHP 3.1 v 
Run Code Online (Sandbox Code Playgroud)



分页链接是错误的,看起来像这样:

localhost/controller-name/index/seo-variable?page=1
localhost/controller-name/index/seo-variable?page=2
localhost/controller-name/index/seo-variable?page=3
Run Code Online (Sandbox Code Playgroud)


我尝试了此操作,但不幸的是它没有帮助:

<?php $this->Paginator->options(['url' => ['controller' => 'ControllerNames', 'action' => 'index', 'product_id' => $product->id]]); ?>
Run Code Online (Sandbox Code Playgroud)

结果是错误的:

localhost:9080/controller-name?page=1&product_id=2<br>
localhost:9080/controller-name?page=2&product_id=2<br>
localhost:9080/controller-name?page=3&product_id=2<br>
Run Code Online (Sandbox Code Playgroud)

我需要更改正确显示的链接吗?
解决方案A

localhost/seo-url/seo-variable?page=1
localhost/seo-url/seo-variable?page=2
localhost/seo-url/seo-variable?page=3
Run Code Online (Sandbox Code Playgroud)


解决方案B

localhost/controller-name/index/2?page=1
localhost/controller-name/index/2?page=2
localhost/controller-name/index/2?page=3
Run Code Online (Sandbox Code Playgroud)


我的英语不好,但我希望你明白我想要的。

url pagination cakephp

5
推荐指数
0
解决办法
696
查看次数

Cakephp 3 - limit() 和包含模型

我有两个模型,分别命名为 Post 和 Comment,它们由 Post hasMany Comment 和 Comment ownsTo Post 链接。我想获取所有帖子的前五个评论。我会使用这个代码:

$this->Posts->find('all')
     ->contain([
        'Comments' => function($q) {
            return $q
                ->order('created ASC')
                ->limit(5);
}
]);
Run Code Online (Sandbox Code Playgroud)

这与 limit() 一起工作不正确。请帮助解决这个问题。我使用了这个例子: 如何限制每个记录/组包含的关联? 我试着喜欢这个(在 Post 模型中):

$this->hasOne('TopComments', [
    'className' => 'Comments',
    'strategy' => 'select',
    'conditions' => function (\Cake\Database\Expression\QueryExpression $exp, \Cake\ORM\Query $query) {
        $query->leftJoin(
            ['CommentsFilter' => 'comments'],
            [
                'TopComments.post_id = CommentsFilter.post_id',
                'TopComments.created < CommentsFilter.created'
            ]);
        return $exp->add(['CommentsFilter.id IS NULL']);
    }
]);
Run Code Online (Sandbox Code Playgroud)

在邮政控制器中:

$this->Posts->find('all')
     ->contain([
        'TopComments' => function($q) {
            return $q
                ->order('TopComments.created ASC')
                ->limit(5);
}
]); …
Run Code Online (Sandbox Code Playgroud)

limits cakephp find contain cakephp-3.0

3
推荐指数
1
解决办法
5288
查看次数

标签 统计

cakephp ×2

cakephp-3.0 ×1

contain ×1

find ×1

limits ×1

pagination ×1

url ×1