Fat Free Framework - 从查询中加载数据数组

Fat Free Framework - Loading array of data from a query

使用 F3 的 ORM 从我的数据库加载唯一记录没有问题。但是,当我想检索多条记录进行显示或处理时,加载方法仍然只会获取它找到的第一条记录。

我找到了这个 on the docs:

By default, a data mapper's load() method retrieves only the first record that matches the specified criteria. If you have more than one that meets the same condition as the first record loaded, you can use the skip() method for navigation

然而,文档随后仅继续展示如何使用内置的 find 方法操作多个记录,returns 一个对象填充了对数据库条目的引用。

我不想每次都循环遍历多条记录,我想一定有我遗漏的东西,一个人如何用无脂肪做到这一点?在文档中,它甚至引用了代码,您可以在简单的数据库调用后执行以下操作,但我还没有看到如何使用提供的方法执行此操作:

<repeat group="{{ @result }}" value="{{ @item }}">
    <span>{{ @item.brandName  }}</span>
</repeat>

获取一批记录的正确方法是什么?示例如下:

$productList = $products->load(array('created_on >= ?', $date->timestamp)); //Load most recently created products

$productList = $products->find(array('created_on >= ?',$date->timestamp),array('order'=>'created_on'));

在上面的示例中,日期使用 Carbon,假设这是一个正确的时间戳。

我认为没有正确的 方法来检索一堆记录。 SQL 映射器充当游标。查看其工作原理的最佳方法是使用 load 方法:映射器将检索所有匹配的记录并将其焦点设置在第一个记录上。你可以用它做你想做的,然后调用 next 继续下一个:

$products->load(array('created_on >= ?', $date->timestamp));
while (!$products->dry() ) {
    // Do something with the record
    $products->next();
}

find方法将return一个记录数组(实际上它们也是游标,但每个游标只有一个记录,因此您可以将它们视为映射到记录的对象) .在这里,您需要将结果分配给一个新变量,就像您在示例中所做的那样:

$productList = $products->find(array('created_on >= ?',$date->timestamp),array('order'=>'created_on'));
// You can loop over the array to get some info or update the records:
foreach ($productList as $product) {
    // Do something with the record
}

或者直接使用模板中的$productList变量:

<repeat group="{{ @productList }}" value="{{ @item }}">
    <span>{{ @item.brandName  }}</span>
</repeat>