CakePHP查找 - 按字符串到Int? [英] CakePHP Find - Order By String-To-Int?
本文介绍了CakePHP查找 - 按字符串到Int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用CakePHP从数据库中提取照片数组,按照照片标题排序(0,1,2,3 ...)我的查询现在看起来像:
I want to use CakePHP to pull an array of photos from a database, sorted by photo title (0, 1, 2, 3...) My query currently looks something like:
$ss_photos = $this->Asset->find('all',array(
'conditions'=>array('kind'=>'photo'),
'order'=>'title'
));
不幸的是,标题似乎是字符串格式,导致不理想的排序顺序19.jpg,等)。在这种类型的Cake查询中,是否有一种快速方法将title作为int来进行排序?
Unfortunately the titles seem to be in string format, leading to an undesirable sort order (2.jpg after 19.jpg, etc). Is there a quick way to cast 'title' as an int for ordering purposes within a Cake query of this type?
推荐答案
确定这是推荐做法,但在第一次通过似乎工作:
Not sure if this is "recommended practice", but on a first pass it seems to work:
$ss_photos = $this->Asset->find('all',array(
'conditions'=>array('kind'=>'photo'),
'order'=>'Asset.title + 0'
));
有任何意见吗?
这篇关于CakePHP查找 - 按字符串到Int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文