php - 试图调用类“App\Twig\AppExtension”的名为“getDoctrine”的未定义方法
问题描述
我是学习 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”的未定义方法的页面时发生错误。
请帮我提供解决方案
解决方案
我用了这个,现在问题解决了
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;
}
推荐阅读
- java - 如何使用 apache nifi 连接到远程 postgresql
- javascript - 使用带有 json 文件的 select2 ajax 远程数据
- c# - 如何避免计数 - Crystal Report 中的最后一个空白页码
- javascript - 在 ES6 中使用 Gulp 4 - Babel 问题
- python - 为什么我的模型在进行正则化和批量标准化后会过拟合
- linux - 如何在 Raspberry Pi 上重新激活终端?
- android - 触摸问题总是在最前面
- javascript - 为什么App.js中这部分代码不起作用?(我做了一个搜索过滤器)
- javascript - JavaScript 中 setInterval 与 setTimeout 的混淆
- javascript - “状态”类型的参数不能分配给“从不”类型的参数