试图调用名为 "getDoctrine" 的未定义方法 class "App\Twig\AppExtension"
Attempted to call an undefined method named "getDoctrine" of class "App\Twig\AppExtension"
我刚开始学习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) %}
我运行页面时出现错误
试图调用 class "App\Twig\AppExtension" 的名为 "getDoctrine" 的未定义方法 "App\Twig\AppExtension"。
请帮我提供解决方案
getDoctine
是在 AbstractController
(或 ControllerTrait
中定义的函数)并且在 Twig 扩展中不可用。您需要将 doctrine
服务注入您的 class。为简洁起见省略了大部分代码:
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();
// ...
}
}
我用过这个,现在问题解决了
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;
}
我刚开始学习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) %}
我运行页面时出现错误 试图调用 class "App\Twig\AppExtension" 的名为 "getDoctrine" 的未定义方法 "App\Twig\AppExtension"。
请帮我提供解决方案
getDoctine
是在 AbstractController
(或 ControllerTrait
中定义的函数)并且在 Twig 扩展中不可用。您需要将 doctrine
服务注入您的 class。为简洁起见省略了大部分代码:
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();
// ...
}
}
我用过这个,现在问题解决了
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;
}