在CakePHP 3.0中的虚拟字段/实体属性上的分页排序链接 [英] Pagination sort link on a virtual field/entity property in CakePHP 3.0

查看:217
本文介绍了在CakePHP 3.0中的虚拟字段/实体属性上的分页排序链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建分页排序链接 虚拟字段/实体属性 a>在CakePHP 3.0中。

I want to create a pagination sort link on a virtual field/entity property in CakePHP 3.0.

在CakePHP 2.x中,我用来创建虚拟字段,然后在该字段上创建分页排序链接。但是,在CakePHP 3.0中,虚拟字段已替换为< a href =http://book.cakephp.org/3.0/en/orm/entities.html#creating-virtual-properties =nofollow>虚拟实体属性。

In CakePHP 2.x I used to create a virtual field, and then create a pagination sort link on that field. However, in CakePHP 3.0, virtual fields have been replaced by virtual entity properties.

有什么方法可以让我在CakePHP 3.0中工作吗?

Is there any way I can get this working in CakePHP 3.0?

在我的情况下,我有一个first_name和last_name列在虚拟实体媒体资源中合并为full_name 。

In my situation, I have a first_name and last_name column, which are combined as full_name in a virtual entity property. I want to sort on the full_name.

推荐答案

如链接文档中所述,虚拟属性不能在查找中使用。这是通过设计,虚拟属性只存在于实体中,它们是在从数据库中检索数据之后在PHP中构建的。

As stated in the linked docs, virtual properties cannot be used in finds. That's by design, virtual properties only live in entities, they are built in PHP after data has been retrieved from the database.

因此,只是忘了一会儿的虚拟属性,专注于查询和计算列!

So, just forget about virtual properties for a moment, an concentrate on queries and computed columns!

请注意,就像关联模型的列一样,计算列需要在 sortWhitelist 选项中指定以便用于排序!

Note that just like columns of associated models, computed columns need to be specified in the sortWhitelist option in order to be useable for sorting!

Cookbook> Controllers> Components> Pagination>控制哪些字段用于排序

Cookbook > Controllers > Components > Pagination > Control which Fields Used for Ordering

您在这里有一些选项,例如您可以在分页选项中定义计算列

You have some options here, for example you could define computed columns in the pagination options

$this->paginate = [
    // ...
    'sortWhitelist' => [
        'id',
        'first_name',
        'last_name',
        'full_name',
        // ...
    ],
    'fields' => [
        'id',
        'first_name',
        'last_name',
        'full_name' => $Table->query()->func()->concat([
            'first_name' => 'literal',
            'last_name' => 'literal'
        ]),
        // ...
    ],
    'order' => [
        'full_name' => 'DESC'
    ]
];



自定义查找器



可重用的选项是使用自定义查找器

A custom finder

Another, more reusable option would be to use a custom finder

$this->paginate = [
    // ...
    'sortWhitelist' => [
        'id',
        'first_name',
        'last_name',
        'full_name',
        // ...
    ],
    'finder' => 'withFullName',
    'order' => [
        'full_name' => 'DESC'
    ]
];





public function findWithFullName(\Cake\ORM\Query $query, array $options)
{
    $query->select([
        'id',
        'first_name',
        'last_name',
        'full_name' => $this->query()->func()->concat([
            'first_name' => 'literal',
            'last_name' => 'literal'
        ]),
        // ...
    ]);
    return $query;
}




  • Cookbook> Controllers> Components> Pagination>使用Controller :: paginate()
  • Cookbook> ... ORM>检索数据&结果集>自定义查找方法

    • Cookbook > Controllers > Components > Pagination > Using Controller::paginate()
    • Cookbook > ... ORM > Retrieving Data & Results Sets > Custom Finder Methods
    • 也可以直接将查询对象传递给 Controller :: paginate()

      It's also possible to directly pass query objects to Controller::paginate().

      $this->paginate = [
          // ...
          'sortWhitelist' => [
              'id',
              'first_name',
              'last_name',
              'full_name',
              // ...
          ],
          'order' => [
              'full_name' => 'DESC'
          ]
      ];
      
      $query = $this->Table
          ->find()
          ->select([
              'id',
              'first_name',
              'last_name',
              'full_name' => $this->query()->func()->concat([
                  'first_name' => 'literal',
                  'last_name' => 'literal'
              ]),
              // ...
          ]);
      $results = $this->paginate($query);
      

      这篇关于在CakePHP 3.0中的虚拟字段/实体属性上的分页排序链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆