when i submit my form i got this error :
Cannot autowire argument $manager of "App\\Controller\\AdController::create()": it references interface "Doctrine\\Common\\Persistence\\ObjectManager" but no such service exists. You should maybe alias this interface to the existing "doctrine.orm.default_entity_manager" service.
This is in function create in AdController.php:
<?php
namespace App\Controller;
use App\Entity\Ad;
use App\Form\AdType;
use App\Repository\AdRepository;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class AdController extends AbstractController
{
/**
* @Route("/ads", name="ads_index")
*/
public function index(AdRepository $repo)
{
$ads = $repo->findAll();
return $this->render('ad/index.html.twig', [
'ads' => $ads,
]);
}
/**
* @Route("/ads/new", name="ads_create")
*
* @return Response
*/
public function create(Request $request, ObjectManager $manager){
$ad = new Ad();
$form = $this->createForm(AdType::class, $ad);
$form->handleRequest($request);//symfony va faire le lien entre les donne des champs fu formulaire et la variable $ad
if($form->isSubmitted() && $form->isValid() ){
$manager->persist($ad);
$manager->flush();
}
return $this->render("ad/new.html.twig", [
'form' => $form->createView()
]);
}
/**
* @Route("/ads/{slug}", name="ads_show")
* @return Response
*/
public function show(Ad $ad){
return $this->render('ad/show.html.twig', [
'ad' => $ad
]);
}
}
and this is my AdType.php :
<?php
namespace App\Form;
use App\Entity\Ad;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
class AdType extends AbstractType
{
/**
* @param string $label
* @param string $placeholder
* @return array
*/
private function getConfiguration($label, $placeholder){
return [
'label' => $label,
'attr' => [
'placeholder' => $placeholder
]
];
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class, $this->getConfiguration("titre", "tapez un super titre pour votre annonce"))
->add('slug', TextType::class, $this->getConfiguration("Adresse web", "tapez l'adresse web (automatique)"))
->add('coverImage', UrlType::class, $this->getConfiguration("Url de l'image principal", "Donnez l'adresse d'une image qui donne vraiment envie"))
->add('introduction', TextType::class, $this->getConfiguration("introduction", "donnez une description global de l'annonce"))
->add('content', TextareaType::class, $this->getConfiguration("Description detaille", "tapez une description qui donne vraiment envie de venir chez vous !"))
->add('price', MoneyType::class, $this->getConfiguration("Prix par nuit", "indiquez le prix que voulez pour une nuit"))
->add('rooms', IntegerType::class, $this->getConfiguration("Nombre de chambre", "le nom de chambres disponibles"))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Ad::class,
]);
}
}
Why i get this error when i submit my form and how can i solve this problem
Thank you in advance
You should avoid using directly the service. Always use the contract instead. It is available for every services
So instead of using ObjectManager
directly, use EntityManagerInterface
The error actually says you should alias that class to an existing service. That happens when Symfony does not know which implementation of interface you are going to use.
Try something like this:
Doctrine\Common\Persistence\ObjectManager: '@doctrine.orm.default_entity_manager'
Add it in services.yml and try.
docs: https://symfony.com/doc/current/service_container/autowiring.html#using-aliases-to-enable-autowiring
use ManagerRegistry
service instead of ObjectManager
/**
* @Route("/ads/new", name="ads_create")
*
* @return Response
*/
public function create(Request $request, ManagerRegistry $managerRegistry){
$ad = new Ad();
$form = $this->createForm(AdType::class, $ad);
$form->handleRequest($request);//symfony va faire le lien entre les donne des champs fu formulaire et la variable $ad
if($form->isSubmitted() && $form->isValid() ){
$em = $managerRegistry->getManager();
$em->persist($ad);
$em->flush();
}
return $this->render("ad/new.html.twig", [
'form' => $form->createView()
]);
}
try with this:
use Doctrine\ORM\EntityManagerInterface;
class Someclass {
protected $em;
public function __construct(EntityManagerInterface $entityManager)
{
$this->em = $entityManager;
}
public function somefunction() {
$em = $this->em;
...
}
}
You must change this use:
use Doctrine\Common\Persistence\ObjectManager;
to this use:
use Doctrine\Persistence\ObjectManager;
Try changing this
$manager->persist($ad);
$manager->flush();
To this
$entityManager = $this->getDoctrine()->getManager();
$entityManager->flush();
尝试使用EntityManagerInterface
而不是ObjectManager
。
You must change this:
$manager->persist($ad);
$manager->flush();
To this:
$entityManager = $this->getDoctrine()->getManager();
$entityManager->flush();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.