简体   繁体   English

类 Doctrine\Common\Persistence\ObjectManager 不存在

[英]Class Doctrine\Common\Persistence\ObjectManager does not exist

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;大家好,我在一些 symfony 项目中工作,但这个错误仍然显示给我,我已将 Doctrine\Common\Persistence\ObjectManager 更改为 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仍然无法工作,请我需要一些帮助,这是为了我的大学考试,我尝试了很多方法来解决这个问题,即使我在 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
    ]);
}

}

You most likely need to use Doctrine\Persistence\ObjectManager instead of Doctrine\Common\Persistence\ObjectManager which has been removed in newer versions of Doctrine.您很可能需要使用Doctrine\Persistence\ObjectManager而不是Doctrine\Common\Persistence\ObjectManager ,后者已在较新版本的 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 .但是,该类没有别名(您可以使用bin/console debug:autowiring doctrine检查)所以我想最简单的方法是使用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
        ]);
    }

}

我有同样的问题,这是新的路径:

use Doctrine\Persistence\ObjectManager;

There are no classes related to persistence in the Doctrine\Common namespace. Doctrine\Common命名空间中没有与持久性相关的类。 So you have to use ie Doctrine\DBAL .所以你必须使用即Doctrine\DBAL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Symfony 4:无法自动装配参数 $manager of ... 它引用接口“Doctrine\\Common\\Persistence\\ObjectManager” - Symfony 4 : Cannot autowire argument $manager of ... it references interface "Doctrine\Common\Persistence\ObjectManager" 类不存在学说 - Class does not exist Doctrine ObjectManager方法createQuery()似乎不存在 - ObjectManager The method createQuery() does not seem to exist getRepository原则中不存在类 - Class does not exist in getRepository doctrine 安装 symfony 项目时出现“类 Doctrine\Common\Cache\ArrayCache 不存在” - 'Class Doctrine\Common\Cache\ArrayCache does not exist' when installing a symfony project Slim 3 + Doctrine 2:类“用户”不存在,MappingException - Slim 3 + Doctrine 2: Class 'User' does not exist, MappingException 具有教义ORM的MappingException-类不存在 - MappingException with Doctrine ORM - Class does not exist PHP致命错误:找不到类&#39;Doctrine \\ Common \\ Persistence \\ Mapping \\ Driver \\ FileDriver&#39; - PHP Fatal error: Class 'Doctrine\Common\Persistence\Mapping\Driver\FileDriver' not found Doctrine\Common\Persistence\Mapping\Driver 丢失 - Doctrine\Common\Persistence\Mapping\Driver is missing 类xx中的注释@Doctrine \\ ORM \\ Mapping不存在,或者无法加载 - The annotation @Doctrine\ORM\Mapping in class xx does not exist, or cannot be loaded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM