dongsheng1238 2016-10-19 20:42
浏览 53
已采纳

Symfony 3 - 查找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

  • 写回答

1条回答 默认 最新

  • doumalu9257 2016-10-20 04:15
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决