Cakephp 2 找到邻居环绕
Cakephp 2 find neighbors wrap around
我正在使用 cakephp 2,幸运的是 find('neighbors') 仍然存在。
然而,我想要的是它环绕。
所以如果我按 id 排序并且我 select 第一个 id,我想将下面的 id 记录作为下一个(有效)但作为以前的最高 id 记录(returns null如果您有第一个 ID)。反之亦然,如果您 select 最高 ID,我希望下一个 ID 最低。[=10=]
有没有办法轻松实现?
如果 find('neighbors')
没有 return 下一个或上一个数组,您可以轻松地自己获取下一个或上一个条目。这是一个示例实现:
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);
}
我正在使用 cakephp 2,幸运的是 find('neighbors') 仍然存在。 然而,我想要的是它环绕。
所以如果我按 id 排序并且我 select 第一个 id,我想将下面的 id 记录作为下一个(有效)但作为以前的最高 id 记录(returns null如果您有第一个 ID)。反之亦然,如果您 select 最高 ID,我希望下一个 ID 最低。[=10=]
有没有办法轻松实现?
如果 find('neighbors')
没有 return 下一个或上一个数组,您可以轻松地自己获取下一个或上一个条目。这是一个示例实现:
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);
}