Kanen Nova Wed Nov 14 00:58:41 -0500 2012

Subject: PHP-AR and Twig

I'm trying to get PHP-AR to work with Twig. In a relational manner.

$foo = Foo::all();

Twig HTML:
{% for bar in foo %} {{ bar.car.model }} {% endfor %}

I can't get it to work. Please help!

Andre Venter Wed Nov 28 22:24:30 -0500 2012

Hi Kanen,

PHP-AR returns an array of objects. You cannot iterate them directly with Twig without writing a plugin for it.

But you can prepare the data in the way Twig expects before passing it to Twig.

You could have a function that converts every object into a "sub" array like this.

function convertMultiplePhpActiveRecordObjectsToArray($objectsList){
// $objectsList is what you get back from PHP-AR
$complexArray = array();
foreach ($objectsList as &$result) {
$complexArray [] = (array)$result->to_array();
return $complexArray; // this should work for Twig