Class Doctrine\Common\Persistence\ObjectManager 不存在

Class Doctrine\Common\Persistence\ObjectManager does not exist

大家好,我在某个 symfony 项目中工作,这个错误仍然显示给我,我已将 Doctrine\Common\Persistence\ObjectManager 更改为 Doctrine\ORM\EntityManagerInterface;仍然没有工作 我需要一些帮助,这是为了我的大学考试,我尝试了很多来解决这个问题,即使我查阅了 stackoerflow 的解决方案,但我没有找到任何可以帮助的东西

    <?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Article;
use App\Repository\ArticleRepository;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\Common\Persistence\ObjectManager;
class BlogController extends AbstractController
{
/**
 * @Route("/blog", name="blog")
 */
public function index(ArticleRepository $repo): Response
{
   // $repo = $this->getDoctrine()->getRepository(Article::class);
    $articles = $repo->findAll();

    return $this->render('blog/index.html.twig', [
        'controller_name' => 'BlogController',
        'articles' => $articles
    ]);
}
/**
 * @Route("/",name="home")
 */
public function home(){
    return $this->render("blog/home.html.twig",[
        "title"=> "miral",
        "age" => 31
    ]);
}

   /**
 * @Route("/blog/new", name="blog_create")
 */
public function create(Request $request, ObjectManager $manager){
    dump($request);
    if($request->request->count() > 0){
        $article = new Article();
        $article->setTitle($request->request->get('title'))
        ->setContent($request->request->get('content'))
        ->setImage($request->request->get('image'));
        $manager->persist($article);
        $manager->flush();
    }
    return $this->render("blog/create.html.twig");

}

/**
 * @Route("/blog/{id}",name="blog_show")
 */
//ArticleRepository $repo, $id
public function show(Article $article){
    //$repo=$this->getDoctrine()->getRepository(Article::class);
   // $article= $repo->find($id);
    return $this->render("blog/show.html.twig",[
        'article' => $article
    ]);
}

}

Doctrine\Common 命名空间中没有与持久性相关的 类。所以你必须使用 Doctrine\DBAL.

您很可能需要使用 Doctrine\Persistence\ObjectManager 而不是 Doctrine\Common\Persistence\ObjectManager,后者已在较新版本的 Doctrine 中被删除。但是,class 没有别名(您可以使用 bin/console debug:autowiring doctrine 检查)所以我想最简单的方法是使用 Doctrine\ORM\EntityManagerInterface.

试试这个。

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Article;
use App\Repository\ArticleRepository;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManagerInterface;

class BlogController extends AbstractController
{
    /**
     * @Route("/blog", name="blog")
     */
    public function index(ArticleRepository $repo): Response
    {
        $articles = $repo->findAll();

        return $this->render('blog/index.html.twig', [
            'controller_name' => 'BlogController',
            'articles'        => $articles
        ]);
    }

    /**
     * @Route("/",name="home")
     */
    public function home()
    {
        return $this->render("blog/home.html.twig", [
            "title" => "miral",
            "age"   => 31
        ]);
    }

    /**
     * @Route("/blog/new", name="blog_create")
     */
    public function create(Request $request, EntityManagerInterface $manager)
    {
        if ($request->request->count() > 0) {
            $article = new Article();
            $article->setTitle($request->request->get('title'))
                ->setContent($request->request->get('content'))
                ->setImage($request->request->get('image'))
            ;
            $manager->persist($article);
            $manager->flush();
        }

        return $this->render("blog/create.html.twig");

    }

    /**
     * @Route("/blog/{id}",name="blog_show")
     */
    public function show(Article $article)
    {
        return $this->render("blog/show.html.twig", [
            'article' => $article
        ]);
    }

}