我正在从数据库中获取限制为30的随机订单的视频ID,但想显示带有desc in Cake php 3中发行日期的视频。请协助
实际上,您的问题非常模糊,您没有提供任何代码,这就是为什么您会被投票。下次,尝试 show 您尝试了一些事情,因为即使您这样做,这也是一个长时间的问题。
我理解:
- 您想从某个数据库中获得30个随机视频;
- 您想通过发布日期(DESC)对它们进行排序。
所以,尝试:
// Retrieve all ids
$ids = $this->Videos->find('list')->toArray();
$ids = array_keys($ids);
// Select 30 random ids from the ids list
$total = count($ids);
$count = 30 <= $total ? 30 : $total;
$selectedIds = [];
for($i = 0; $i < $count; $i++) {
$newId = -1;
do {
$newId = rand(0, $total - 1);
} while(!in_array($newId, selectedIds));
$selectedIds[] = $newId;
}
// Now you got your ids in an array
$videos = $this->Videos->find('all')
->where(['id' => $selectedIds])
->order(['publish_date' => 'DESC'])
->limit(30);