【问题标题】:Attempted to call an undefined method named "getDoctrine" of class "App\Twig\AppExtension"试图调用类“App\Twig\AppExtension”的名为“getDoctrine”的未定义方法
【发布时间】:2019-08-06 02:48:50
【问题描述】:

我是学习 symfony4 的新手。我在树枝扩展中使用该学说时遇到问题。如何在树枝扩展中使用教义查询。

请帮我如何配置此代码的服务


namespace App\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;

class AppExtension extends AbstractExtension
{
     public function getFilters(): array
    {
        return [
            // If your filter generates SAFE HTML, you should add a third
            // parameter: ['is_safe' => ['html']]
            // Reference: https://twig.symfony.com/doc/2.x/advanced.html#automatic-escaping
            new TwigFilter('filter_name', [$this, 'doSomething']),
        ];
    }

    public function getFunctions(): array
    {
        return [
            new TwigFunction('followed', [$this, 'doSomething']),
        ];
    }

    public function doSomething($id, $admin)
    {
        // ...
        $follower = $this->getDoctrine()->getRepository(Follower::class)->findAll();
        foreach( $follower as $value ){
            if($value['user']==$admin && $value['followed_user']==$id) return false;
        }
        return true;
    }
}

这是我的树枝功能代码

{% if followed(users.id, app.user.id) %}

运行页面时出现错误 试图调用“App\Twig\AppExtension”类的名为“getDoctrine”的未定义方法。

请帮我提供解决方案

【问题讨论】:

    标签: php doctrine-orm doctrine symfony4


    【解决方案1】:

    我用了这个,现在问题解决了

        use Doctrine\Common\Persistence\ManagerRegistry;
    
        public function doSomething($id, $admin)
        {
            // ...
            $follower = $this->em->getRepository(Follower::class)->findBy([
                'followed_user' => $id,
                'user' => $admin
            ]);
    
            if(sizeof($follower)>0) return false;
            else return true;
        }
    

    【讨论】:

      【解决方案2】:

      getDoctine 是在AbstractController(或ControllerTrait 准确地说)中定义的函数,在 Twig 扩展上不可用。您需要将doctrine 服务注入您的班级。为简洁起见,您的大部分代码都被省略了:

      use Doctrine\Common\Persistence\ManagerRegistry;
      
      class AppExtension extends AbstractExtension
      {
          private $em;
      
          public function __construct(ManagerRegistry $registry)
          {
              $this->em = $registry;
          }
      
          public function doSomething($id, $admin)
          {
              $follower = $this->em->getRepository(Follower::class)->findAll();
              // ...
          }
      }
      

      【讨论】:

      • 现在发生的错误是在模板的渲染过程中抛出了异常(“Class App\Twig\Follower 不存在”)
      • 我的关注者的命名空间是 App\Entity\Follower
      • 缺少该类的 use 指令,但您也可以将其作为字符串传递而不包含它 getRepository('App\Entity\Follower')(尽管我不推荐它,您的 IDE 中的重构工具不会工作)。
      • 再次出现错误消息是 Cannot use object of type of App\Entity\Follower as array
      • @babloo 这与教义无关。 $value 是一个对象,您正在使用数组访问来获取属性 $value['user']。使用`$value->getUser()。
      猜你喜欢
      • 2020-07-27
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      • 2018-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多