Cak*_*PHP 0 php cakephp cakephp-2.0
我在CakePHP中遇到了问题
我有一个数组($ A)及其值
Array // in $A
(
[0] => 3
[1] => 4
...
}
Run Code Online (Sandbox Code Playgroud)
在控制器内:
$conditions[]=array('Room.place_id' =>$A,'Room.status'=>'1');
$this->paginate= array(
'limit' =>50,
'recursive' => 2,
'conditions' => $conditions,
);
Run Code Online (Sandbox Code Playgroud)
现在的结果是:
Array
(
[0] => Array
(
[Room] => Array
(
[id] => 1
[place_id] => 1
[type] => place
)
)
[1] => Array
(
[Room] => Array
(
[id] => 2
[place_id] => 3
)
).
...
Run Code Online (Sandbox Code Playgroud)
现在我想使用$ A值来订购(在地方ID)...
我希望第一条记录是[place_id] => 3的房间(因为$ a [0] = 3)
第二条记录应该是[place_id] => 4的房间(因为$ a [1] = 4),依此类推
在mysql中,您可以按特定字段值排序,方法是ORDER BY FIELD:
SELECT * FROM rooms WHERE place_id IN (3,1) ORDER BY FIELD(place_id, 3, 1);
Run Code Online (Sandbox Code Playgroud)
这会为了你的房间在你想要自定义顺序(先那些place_id=3然后那些 place_id=1).
要做蛋糕:
$this->paginate= array(
'order' => 'FIELD(Room.place_id, '.implode(',', $A).')',
...
Run Code Online (Sandbox Code Playgroud)
我假设$A只包含数字,否则你将不得不清理FIELD表达式中的值.