Search code examples
fat-free-framework

Illegal string offset in Fat Free Framework template


I fetch a result in Fat Free Framework with this:

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

Then I walk through $product using the dry() method and do some calculations to some of it's fields. I want to make the re-calculated content of the $product available in my template, so I do:

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

Now, when in my template I do:

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

I get this error:

Internal Server Error
Illegal string offset 'productName'

I discovered that my {{@product_contents}} is a mapper object and not an array, hence the error.

The question is: how can I still use the contents of $product in my template in repeat groups?


Solution

  • The cast() method is there to cast a mapper object to an array:

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