脚本宝典收集整理的这篇文章主要介绍了CakePHP 3.0中虚拟字段/实体属性上的分页排序链接,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
在CakePHP 2.x中,我曾经创建了一个virtual field,然后在该字段上创建了一个分页排序链接.但是,在CakePHP 3.0中,virtual fields已被virtual entity properties取代.
在我的情况下,我有一个First_name和last_name列,它们在virtual entity property中组合为full_name.我想对full_name进行排序.
需要通过sortWhITelist指定计算列
就像关联模型的列一样,需要在sortWhitelist选项中指定计算列,以便可用于排序!
Cookbook > Controllers > Components > Pagination > Control which Fields Used for Ordering
通过分页选项
这里有一些选项,例如您可以在分页选项中定义计算列:
$this->paginate = [ // ... 'sortWhitelist' => [ 'id','first_name','last_name','full_name',// ... ],'fields' => [ 'id','full_name' => $this->Table->query()->func()->concat([ 'first_name' => 'literal','last_name' => 'literal' ]),'order' => [ 'full_name' => 'DESC' ] ];
定制查找器
$this->paginate = [ // ... 'sortWhitelist' => [ 'id','finder' => 'withFullName','order' => [ 'full_name' => 'DESC' ] ];
public function findWithFullName(\Cake\ORM\Query $query,array $options) { return $query->select([ 'id','full_name' => $query->func()->concat([ 'first_name' => 'literal',// ... ]); }
> Cookbook > Controllers > Components > Pagination > Using Controller::paginate()
> Cookbook > … ORM > Retrieving Data & Results Sets > Custom Finder Methods
也可以直接将查询对象传递给Controller :: paginate():
$this->paginate = [ // ... 'sortWhitelist' => [ 'id','order' => [ 'full_name' => 'DESC' ] ]; $query = $this->Table ->find() ->select(function (\Cake\ORM\Query $query) { return [ 'id','full_name' => $query->func()->concat([ 'first_name' => 'literal','last_name' => 'literal' ]),// ... ]; }); $results = $this->paginate($query);
以上是脚本宝典为你收集整理的CakePHP 3.0中虚拟字段/实体属性上的分页排序链接全部内容,希望文章能够帮你解决CakePHP 3.0中虚拟字段/实体属性上的分页排序链接所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。