Fat Free Framework 模板中的非法字符串偏移

Illegal string offset in Fat Free Framework template

我用这个在 Fat Free Framework 中获取结果:

$product = new DB\SQL\Mapper($db,'products');    
$product->load(array('productId=:ID',':ID'=>$productId));

然后我使用 dry() 方法遍历 $product 并对它的某些字段进行一些计算。我想让 $product 的重新计算内容在我的模板中可用,所以我这样做:

$f3->set('product_contents', $product);

现在,当我在我的模板中时:

<repeat group ="{{@product_contents}}" value="{{@item}}">
   <p>{{@item.productName}}</p>
</repeat>

我收到这个错误:

Internal Server Error
Illegal string offset 'productName'

我发现我的 {{@product_contents}} 是一个映射器对象而不是数组,因此出现错误。

问题是: 如何在重复组中仍然使用模板中 $product 的内容?

cast() 方法用于将映射器对象转换为数组:

$f3->set('product_contents', $product->cast());

只需将结果加载到变量中
$output = $product->load(array('productId=:ID',':ID'=>$productId));
$f3->set('product_contents', $output);