Class Doctrine\Common\Persistence\ObjectManager does not exist

Viewed 472

hi guys so im working in some symfony project and this error still display to me i have changed the Doctrine\Common\Persistence\ObjectManager to Doctrine\ORM\EntityManagerInterface; and still not working please i need some help it's for my university exams i tried alot to fix this issues even i look it up to stackoerflow about a solution but i didn't find anything can help

    <?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
    ]);
}

}
3 Answers

You most likely need to use Doctrine\Persistence\ObjectManager instead of Doctrine\Common\Persistence\ObjectManager which has been removed in newer versions of Doctrine. However, there is no alias for that class (you can check that with bin/console debug:autowiring doctrine) so I guess the easiest way would be to use Doctrine\ORM\EntityManagerInterface.

Try this.

<?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
        ]);
    }

}

I had the same issue, here is the new path :

use Doctrine\Persistence\ObjectManager;

There are no classes related to persistence in the Doctrine\Common namespace. So you have to use i.e. Doctrine\DBAL.

Related