0

i've a situation where my php controller return an array from database table "messages" (db has no INNER JOIN with table users!):

$messages = get_messages($filters);

var_dump($messages);

//output var_dump() :
// array(
//   0 => array(
//      'message' => 'text sample',
//      'userID' => 1
//     )
//   1 => array(
//      'message' => 'other text sample',
//      'userID' => 5
//     )
// )

now inside TWIG i use a loop like this:

{% for mex in messages %}
Message: {{ mex.message }}
From user: {{ mex.userID }}
{% endfor %}

My question is: what's the best method to print "name of a user" instead their ID without create a php call function inside the twig loop? example i don't want to use:

{% for mex in messages %}
Message: {{ mex.message }}
From user: {{ php_function_to_retrieve_name_of_user_from_id(mex.userID) }}
{% endfor %}

Of course i can create an inner join from messages table and users table... but i'd like to know if is there a "universal" approach when inside a twig loop i need to retrieve info from others tables in database.... thanks

itajackass
  • 346
  • 1
  • 2
  • 15
  • I'm guessing you are using `twig` as a stand-alone package? – DarkBee Apr 05 '22 at 13:55
  • @DarkBee i'm actually trying to learn Symfony (actually i used only pure PHP) – itajackass Apr 05 '22 at 14:04
  • 1
    You should have a look at/read into [doctrine](https://symfony.com/doc/current/doctrine.html) then, which is basically an [ORM](https://stackoverflow.com/questions/1279613/what-is-an-orm-how-does-it-work-and-how-should-i-use-one). – DarkBee Apr 05 '22 at 14:10

0 Answers0