当我提交表格时,我收到此错误:
无法自动连线参数 $manager "App\Controller\AdController::create((":它引用接口 "Doctrine\Common\Persistence\ObjectManager",但不存在此类服务。您可能应该将此接口别名为现有的"doctrine.orm.default_entity_manager"服务。
这是在 AdController 中创建的函数.php:
<?php
namespace AppController;
use AppEntityAd;
use AppFormAdType;
use AppRepositoryAdRepository;
use SymfonyComponentHttpFoundationRequest;
use DoctrineCommonPersistenceObjectManager;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;
use SymfonyComponentFormExtensionCoreTypeSubmitType;
use SymfonyComponentHttpFoundationSessionSessionInterface;
use SymfonyBundleFrameworkBundleControllerAbstractController;
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
]);
}
}
这是我的广告类型.php :
<?php
namespace AppForm;
use AppEntityAd;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use SymfonyComponentFormExtensionCoreTypeUrlType;
use SymfonyComponentFormExtensionCoreTypeTextType;
use SymfonyComponentFormExtensionCoreTypeMoneyType;
use SymfonyComponentFormExtensionCoreTypeIntegerType;
use SymfonyComponentFormExtensionCoreTypeTextareaType;
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,
]);
}
}
为什么我在提交表单时收到此错误以及如何解决此问题
提前谢谢你
您应该避免直接使用该服务。始终改用合同。 它适用于所有服务
因此,与其直接使用ObjectManager
,不如使用EntityManagerInterface
该错误实际上表明您应该将该类别名为现有服务。当Symfony不知道你将使用哪种接口实现时,就会发生这种情况。
尝试这样的事情:
DoctrineCommonPersistenceObjectManager: '@doctrine.orm.default_entity_manager'
将其添加到services.yml中并尝试。
文档:https://symfony.com/doc/current/service_container/autowiring.html#using-aliases-to-enable-autowiring
使用ManagerRegistry
服务而不是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()
]);
}
尝试这样做:
use DoctrineORMEntityManagerInterface;
class Someclass {
protected $em;
public function __construct(EntityManagerInterface $entityManager)
{
$this->em = $entityManager;
}
public function somefunction() {
$em = $this->em;
...
}
}
您必须更改此用法:
use DoctrineCommonPersistenceObjectManager;
为此用途:
use DoctrinePersistenceObjectManager;
尝试更改此设置
$manager->persist($ad);
$manager->flush();
对此
$entityManager = $this->getDoctrine()->getManager();
$entityManager->flush();
尝试使用EntityManagerInterface
而不是ObjectManager
。
您必须更改此设置:
$manager->persist($ad);
$manager->flush();
对此:
$entityManager = $this->getDoctrine()->getManager();
$entityManager->flush();