grosfaignan / symfonyTutorial

https://www.youtube.com/watch?v=UTusmVpwJXo tutorial following
GNU General Public License v3.0
1 stars 0 forks source link

ObjectManager not referenced #1

Open grosfaignan opened 4 years ago

grosfaignan commented 4 years ago

problem appear when trying to instance ObjectManager class, ti seems to be aliassed by

"Cannot autowire argument $manager of "App\Controller\BlogController::create()": it references interface "Doctrine\Persistence\ObjectManager" but no such service exists. You should maybe alias this interface to the existing "doctrine.orm.default_entity_manager" service."

Problem appear after adding

public function create(Request $request, ObjectManager $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'))
                ->setCreatedAt(new \DateTime());
            $manager->persist($article);
            $manager->flush();
        }
        return $this->render('blog/create.html.twig');
    }

in

C:\wamp64\www\tutoSymfony\demo\src\Controller\BlogController.php

problem appear cause object manager seems to need an alias to be found

grosfaignan commented 4 years ago

solved by :

adding

Doctrine\Persistence\ObjectManager: '@doctrine.orm.default_entity_manager'

into :

C:\wamp64\www\tutoSymfony\demo\config\services.yaml

grosfaignan commented 4 years ago

need to find why ObjectManager need '@doctrine.orm.default_entity_manager' as alias

leonlombard commented 4 years ago

You can type-hint with EntityManagerInterface instead of ObjectManager. I found this out when defining a Symfony as a service

domoquick commented 4 years ago

if you solveding:

use Doctrine\Persistence\ManagerRegistry; ... public function create(Request $request, ManagerRegistry $manager) { if ($request->request->count() > 0) { $article = new Article(); $article->setTitle($request->request->get('title')) ... $manager->getManager()->persist($article); $manager->getManager()->flush(); } return $this->render('blog/create.html.twig'); }

Ferdinand-Lohalo commented 3 years ago

if you solveding:

use Doctrine\Persistence**ManagerRegistry; ... public function create(Request $request, ManagerRegistry $manager) { if ($request->request->count() > 0) { $article = new Article(); $article->setTitle($request->request->get('title')) ... $manager->getManager()->persist($article); $manager->getManager()**->flush(); } return $this->render('blog/create.html.twig'); }

domoquick

Your answer was of great importance to me as a newbie in symfony. So she solved my problem perfectly. Thanks to you