我正在使用cakephp 2,幸运的是,查找('邻居')仍然存在。然而,我想要的是它包裹着。
因此,如果我按id排序并选择第一个id,我希望下一个id记录(作品),但是之前的最高id记录(如果你有第一个id,则返回null)。反之亦然,如果您选择最高ID,我希望下一个ID最低。
有没有办法轻松实现这一目标?
如果find('neighbors')
没有返回下一个或上一个数组,您可以自己轻松地抓住下一个或上一个条目。这是一个示例实现:
public function view($id) {
// Find neighboring products by 'id'
$neighbors = $this->Product->find('neighbors', array(
'field' => 'id',
'value' => $id
));
// If the "prev" neighbor returns null, then assign it to the
// last item in the model as sorted by id.
if ($neighbors['prev'] === null)
{
$neighbors['prev'] = $this->Product->find('first', array(
'order' => array('Product.id' => 'desc')
));
// If the "next" neighbor returns null, then assign it to the
// first item in the model as sorted by id.
} elseif ($neighbors['next'] === null) {
$neighbors['next'] = $this->Product->find('first', array(
'order' => array('Product.id' => 'asc')
));
}
// Set the data to 'neighbors' for use in the view.
$this->set('neighbors', $neighbors);
}