kohana 查找集合的第二个元素(使用 find())

发布于 2024-11-05 08:08:52 字数 219 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

維他命╮ 2024-11-12 08:08:52
$first_two = $product->images->limit(2)->find_all();

find() 与 find_all() 基本相同,添加了 limit(1) 并返回当前结果(第一个)。

$first = current($first_two);
$second = next($first_two);

请注意,next() 会推进内部数组指针,因此如果您想再次从头开始循环,则必须重置() 它。

$first_two = $product->images->limit(2)->find_all();

find() is basically the same as find_all(), adding a limit(1) and returning the current result (first one).

$first = current($first_two);
$second = next($first_two);

Notice that next() advances the internal array pointer so you'll have to reset() it if you want to loop from the start again.

半山落雨半山空 2024-11-12 08:08:52

您可以使用 limit($n)offset($o) 组合来选择从 $o 开始的 $n 行代码> DB 中的位置。在此处了解有关查询生成器方法的更多信息。

因此,您的代码将类似于 $image = $product->images->offset(1)->find();

PS。请注意,某些数据库引擎可能不支持 SQL 的 OFFSET 语句(例如 MS SQL 服务器)。

You can use limit($n) and offset($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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文