【问题标题】:Doctrine connection in Twig extension - SymfonyTwig 扩展中的教义连接 - Symfony
【发布时间】:2013-07-03 07:26:42
【问题描述】:

根据我在网上阅读的内容,应该可以在 Twig 扩展文件中创建 Doctrine 连接。我想创建一个带有过滤器的扩展,它将接收类别的 id,然后返回该类别在数据库中建立的类别树中的位置。

树枝扩展文件:

(Symfony/src/MyProject/AdminBundle/Twig/MyExtension.php)

<?php

namespace MyProject\AdminBundle\Twig;

class ToolboxExtension extends \Twig_Extension
{
    protected $em;

    public function __construct($em)
    {
        $this->em = $em;
    }

    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('path', array($this, 'categoryPath'))
        );
    }

    public function categoryPath($category_id) {
        $repository = $this->$em->getRepository('MyProjectAdminBundle:Category');
        $category = $repository->findOneById($category_id);
        $path = $repository->getPath($category);
        return implode(">", $path);
        return $category;
    }

    public function getName()
    {
        return 'toolbox_extension';
    }

}

服务配置文件:

(Symfony/src/MyProject/AdminBundle/Resources/config/services.yml)

services:
    myproject.twig.toolbox_extension:
        class: MyProject\AdminBundle\Twig\MyExtension
        tags:
            - { name: twig.extension }
        arguments:
            em: "@doctrine.orm.entity_manager"

但是每当我使用这个过滤器 categoryPath 时,Twig 就会崩溃。所以模板只会在第一次使用这个扩展之前加载。

【问题讨论】:

  • 您能否添加更多信息 - 它崩溃了 真的没有帮助.. 到底发生了什么?日志中有哪些错误(symfony/php/web 服务器)?屏幕上显示什么?
  • 我之前没有在服务定义中看到命名参数。如果他们是合法的,懒得去查。如果他们是,那就太好了。无论如何,请尝试将 em: 替换为 -。此外,$this->$em 应该是 $this->em。最后,考虑注入存储库而不是实体管理器。无需知道类别类名称。
  • ManseUK:它没有返回任何特定的错误或警告,HTML 响应已成功发送到浏览器。但是响应源只包含模板的那部分,它在过滤器调用之前存在。
  • Cerad:已解决,谢谢。
  • 如果您已经解决了问题,请将解决方案作为答案发布!

标签: symfony doctrine twig


【解决方案1】:

对我来说,以下解决方案非常完美。我在google groups 上找到了帖子,它解决了我在 Twig 扩展中的教义问题。

在我的 AppBundle\Resources\services.yml 中:

app.twig.get_province.extension:
            class: AppBundle\Twig\GetProvinceExtension
            tags:
              - { name: twig.extension }
            arguments: [ '@doctrine.orm.entity_manager' ]

在 AppBundle\Twig\GetProvinceExtention.php 中:

class GetProvinceExtension extends \Twig_Extension {

    /**
     * @var EntityManager
     */
    protected $em;

    /**
     * GetProvinceExtension constructor.
     * @param EntityManager $em
     */
    public function __construct(EntityManager $em) {
        $this->em = $em;
    }

    public function getFilters() {
        return [
            new \Twig_SimpleFilter('province', [$this, 'provinceFilter']),
        ];
    }

    public function provinceFilter($code) {
        $province = $this->em->getRepository("AppBundle:Province")
            ->findOneBy([ "code" => $code ]);

        return $province->getName();
    }
}

【讨论】:

    【解决方案2】:

    尝试将$this-&gt;em-&gt; 替换为$this-&gt;$em-&gt;

    【讨论】:

    • 同时尝试在你的构造函数中添加EntityManager,而不仅仅是变量
    【解决方案3】:

    尝试替换

    $this->em-> 
    

    $this-&gt;$em-&gt; 并替换

    $this->em = $em; 
    

    $this->em = $em['em'];
    

    和 论据:

    em: "@doctrine.orm.entity_manager"
    

    与 参数:[em: "@doctrine.orm.entity_manager"]

    【讨论】:

      【解决方案4】:

      你可以使用

      {% set Products = repository('Entity\\Products').findAll() %}
      

      在你的树枝里

      然后你可以通过使用来做一个foreach

      {% Product in Products %}
           <p>{{ Product.name }}</p>
      {% endfor %}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-09
        • 2018-11-06
        相关资源
        最近更新 更多