假设我有这样的查询:
(SELECT * FROM user WHERE id < 5 order by id DESC LIMIT 1)
UNION
(SELECT * FROM user WHERE id = 5)
UNION
(SELECT * FROM user WHERE id > 5 LIMIT 1)
如何将上述查询转换为CakePHP find('all')查询?
谢谢
假设我有这样的查询:
(SELECT * FROM user WHERE id < 5 order by id DESC LIMIT 1)
UNION
(SELECT * FROM user WHERE id = 5)
UNION
(SELECT * FROM user WHERE id > 5 LIMIT 1)
如何将上述查询转换为CakePHP find('all')查询?
谢谢
将其重写为 find('all') 会有问题,但find('neighbors')
可能符合您的要求;
请参阅文档find('neighbors')
这应该为您提供所需的数据,但您可能需要稍微采用您的代码才能使其与返回数组的不同“布局”正常工作;
$data = $this->User->find('neighbors', array('field' => 'id', 'value' => 5));
更新
刚刚看到您使用的是 CakePHP 1.3,上面发布的链接指向文档的 2.x 部分。虽然类似,但这是 CakePHP 1.3 的文档:
http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Models.html#find-neighbors