p3tch
p3tch

Reputation: 1495

Symfony 3 - best practice for looking up ID

So I have a poll class with a createdBy value (userID of the person who submitted it), then a controller that lists all polls in the poll table

    public function indexAction()
{
    $entityManager = $this->getDoctrine()->getManager();
    $posts = $entityManager->getRepository(Poll::class)->findBy([], ['createdDate' => 'DESC']);

    return $this->render('poll/admin/index.html.twig', ['posts' => $posts]);
}

My twig template looks a little like this at the momemt

        <tbody>
    {% for poll in posts %}
        <tr id="poll_{{ poll.id }}">
            <td> {{ poll.title }}</td>
            <td>{{ poll.createdBy }}</td>
            <td>etc</td>
            <td>etc</td>
            <td>etc</td>
        </tr>
    {% endfor %}
    </tbody>

If I want to display the actual username instead of the createdBy ID, what would the best practice be? I'm using FOSUserBundle

Upvotes: 0

Views: 78

Answers (1)

Don Omondi
Don Omondi

Reputation: 946

Create a simple twig extension that converts an integer to a User Object. Obviously it does this by querying the DB in the background, hence, enable Doctrine's Second Level Cache (assuming you use Doctrine) to not hit the DB every time for user Object. It will also help in the controller when you call $this->getUser()

Sample Twig Extension

<?php

namespace AppBundle\Twig;

use Twig_Extension;
use Twig_SimpleFilter;
use Doctrine\ORM\EntityManager;
use JMS\DiExtraBundle\Annotation\Tag;
use JMS\DiExtraBundle\Annotation\Inject;
use JMS\DiExtraBundle\Annotation\InjectParams;
use JMS\DiExtraBundle\Annotation\Service;

/**
 * @Service("app.twig_extension_hydrate_user" , public=false)
 * @Tag("twig.extension")
 */
class HydrateUserExtension extends Twig_Extension
{
    protected $em;

    /**
     * @InjectParams({
     *     "em" = @Inject("doctrine.orm.entity_manager")
     * })
     */
    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    /**
     * @inheritdoc
     */
    public function getName()
    {
        return 'hydrate_user_extension';
    }

    public function getFilters()
    {
        return array(
            new Twig_SimpleFilter('hydrateUser', array($this, 'hydrateUserFilter')),
        );
    }

    public function hydrateUserFilter($user_id)
    {
        $em = $this->em;
        $user = $em
            ->getRepository('AppBundle:Users')
            ->queryUserById($user_id);
        return $user;
    }

}

Then in a Twig Template as in your example

<tbody>
{% for poll in posts %}
<tr id="poll_{{ poll.id }}">
    <td> {{ poll.title }}</td>
    <td>{{ poll.createdBy|hydrateUser.username }}</td>
    <td>etc</td>
    <td>etc</td>
    <td>etc</td>
</tr>
{% endfor %}
</tbody>

PS: Make sure your clear the your cache even in dev env to ensure code works!

Upvotes: 1

Related Questions