kohana 查找集合的第二个元素(使用 find())
我有一个 kohana 网站,我遇到的情况是我总是需要获取集合的前两个元素(它们实际上是图片)。
我使用 : 非常简单地获取第一个元素,
$image = $product->images->find();
但是我如何才能真正获取第二个元素? (最终使用 find )。有什么简单的解决方案吗?
谢谢!
i have a kohana website and i have a situation where i always need to get the first two elements of a collection (they are actually pictures).
i take the first element very simple using :
$image = $product->images->find();
but how can i actually take the second element? (using find eventually). Is there any easy solution for that?
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(2)
find() 与 find_all() 基本相同,添加了 limit(1) 并返回当前结果(第一个)。
请注意,next() 会推进内部数组指针,因此如果您想再次从头开始循环,则必须重置() 它。
find() is basically the same as find_all(), adding a limit(1) and returning the current result (first one).
Notice that next() advances the internal array pointer so you'll have to reset() it if you want to loop from the start again.
您可以使用
limit($n)
和offset($o)
组合来选择从$o
开始的$n
行代码> DB 中的位置。在此处了解有关查询生成器方法的更多信息。因此,您的代码将类似于
$image = $product->images->offset(1)->find();
PS。请注意,某些数据库引擎可能不支持 SQL 的
OFFSET
语句(例如 MS SQL 服务器)。You can use
limit($n)
andoffset($o)
combination to select$n
rows starting from$o
position in DB. Read more about Query Builder methods here.So, your code will looks like
$image = $product->images->offset(1)->find();
PS. Note than some DB engines may not support SQL's
OFFSET
statement (MS SQL server for example).