【问题标题】:Undefined method 'getDoctrine'.intelephense(1013) Symfony 5未定义的方法'getDoctrine'.intelephense(1013) Symfony 5
【发布时间】:2022-01-02 11:19:36
【问题描述】:

即使我尝试在我的 propertyController 中调用 Doctrine ORM 包,我也有以下错误。我不知道我错过了什么。

未定义的方法'getDoctrine'.intelephense(1013)

namespace App\Controller;

use App\Entity\Property;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;
use  Symfony\Bridge\Doctrine\ManagerRegistry;


class propertyController extends AbstractController
{
    /**
     * @Route("/billets",name="property.index")
     * @return Response
     */
    public function index():Response{
        
        $repostory= $this->getDoctrine()->getRepository(Property::class);
            return $this->render("proprety/index.html.twig",[
                'current_menu' => 'properties'
            ]);
    }
}

【问题讨论】:

  • getDoctrine() 在 5.4 中确实已弃用,但直到 6.0 才被删除,所以我认为上述评论不适用于此处。我会改为查看 these inteliphense questions。

标签: php doctrine-orm controller doctrine symfony5


【解决方案1】:

你应该注入 EntityManager 对象

use Doctrine\ORM\EntityManagerInterface;

...

   public function index(EntityManagerInterface $em): Response
   {
            
        $repostory = $em->getRepository(Property::class);
        return $this->render("proprety/index.html.twig",[
            'current_menu' => 'properties'
        ]);
   }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-19
    • 2021-09-15
    • 1970-01-01
    • 2022-11-17
    • 2021-08-01
    • 2021-12-08
    相关资源
    最近更新 更多