Cakephp2 - 按强数字自然排序

Cakephp2 - natural sorting by strong with numbers

我有这样的疑问:

$a = $this->Gallery->find('all',array (
        'conditions'=>array('Gallery.id'=>$matches[1]),
        'contain' => array('Photo'=>array('fields'=>array('Photo.name'),
            'order'=>array('Photo.name'=>'ASC')
        ))));

此代码通过 ID 和相关照片获取图库。我想按名称对照片进行排序,现在我得到了这个:

1.jpg 11.jpg 12.jpg 2.jpg

因此,如您所见,这不是自然排序。我一直在寻找信息,我该如何存档,但没有成功。我想,我需要修改:

'order'=>array('Photo.name'=>'ASC')

感谢您的帮助。

不确定这是否能完全解决您的问题,但您可以尝试通过先按长度排序来应用 MySQL 数据库的自然排序:-

'order' => array(
    'LENGTH(Photo.name)' => 'ASC',
    'Photo.name' => 'ASC'
)

我认为你需要尝试下面的代码。

只有一点不同。 'order'=>array('FIELD(Photo.name)'=>'ASC')

     $a =   $this->Gallery->find('all',array (
            'conditions'=>array('Gallery.id'=>$matches[1]),
            'contain' => array(
                'Photo'=> array(
                    'fields'=>array('Photo.name'),
                    'order'=>array('FIELD(Photo.name)'=>'ASC')
                ),
                )
            )
        );

希望它对你有用。 如果您没有解决问题,请告诉我。

谢谢。