Search code examples
phpsymfonytwigredbean

Twig - how to natively display RedBean relationships inside template?


I'm trying to get symfony's Twig to play nicely with RedBean.

I can display a top-level bean's data, but none of its relationships.

Here's what I mean:

In my controller, I'm calling Twig in a standard way (somewhat psuedo code):

// Controller
$vars = array(
    'people' = R::find('person')
);

return $this->app['twig']->render('index.twig',$vars);

My beans are structured as follows:

PERSON
->id
->first_name
->last_name
->company <-- (this represents a 'company' bean)

COMPANY
->id
->name

Inside index.twig, I can easily output the person's name like this...

{% for person in people %}
    {{person.first_name}}
{% endfor %}

... but what I WANT to be able to access is the associated company bean, like this...

{% for person in people %}
    **{{person.company.name}}**
{% endfor %}

How do I do that from inside a twig template without requiring additional controller/model logic?


Solution

  • This shows the basic problem:

    protected function testQuery()
    {
        $persons = \R::find('personx');
        foreach($persons as $person)
        {
            //$person->company;
    
            if ($person instanceof \ArrayAccess && isset($person['company']))
            {
                echo 'Got Array' . "\n";
            }
            echo get_class($person) . ' ' . $person->name . ' ' . $person->company->name . "\n";
        }
    }
    

    What is happening is that company is lazy loaded when you do $person->company. Twig checks for the existence of the company property before attempting to load it and does not find it. If you uncomment the $person->company line then the test passes and all will be well.

    I didn't see anything in RedBeans to allow eager loading. You could have your controller run through and just call $person->company on each person. Or you could try messing with Twig_Template::getAttribute(); Or maybe even use the queries and work with arrays.