Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在symfony 5.2中将实体依赖注入控制器?_Php_Symfony_Dependency Injection_Doctrine Orm - Fatal编程技术网

Php 如何在symfony 5.2中将实体依赖注入控制器?

Php 如何在symfony 5.2中将实体依赖注入控制器?,php,symfony,dependency-injection,doctrine-orm,Php,Symfony,Dependency Injection,Doctrine Orm,我是symfony的初学者,我想将我的post实体注入控制器的方法中 但是,我触发了以下错误: Unable to guess how to get a Doctrine instance from the request information for parameter "post". 这是我的密码: /** *@Route(“/post/article/new”,name=“new.html.twig”) *@param Request$Request *@param

我是symfony的初学者,我想将我的post实体注入控制器的方法中

但是,我触发了以下错误:

Unable to guess how to get a Doctrine instance from the request information for parameter "post".
这是我的密码:

/**
*@Route(“/post/article/new”,name=“new.html.twig”)
*@param Request$Request
*@param Posts$Posts
*@返回响应
*/
公共函数newArticle(Request$Request,Posts$Posts):响应
{
$post=$posts;
$article=新文章();
$post->setAuthor(1);
$form=$this->createForm(PostsType::class,$post);
$form->handleRequest($request);
如果($form->isSubmitted()&&&$form->isValid()){
$this->em->persist($post);
$this->em->flush();
$article->setPost($post->getId());
$themes=$form->get('themes')->getData();
$article->setThemes(内爆(',',$themes));
$this->em->persist($post);
$this->em->flush();
返回$this->redirectToRoute('home.html.twig');
}
返回$this->render('post/article.html.twig'[
'formArticle'=>$form->createView()
]);
}

您需要对路由中的帖子进行引用,比如id的slug或另一个唯一字段。
@Route(“/post/{id}/article/new”,…


否则Symfony不知道加载哪个帖子。

将路由更改为/Post/{id}/article/new和id应该是post id,这样param converter将获得与此id相关的post对象,或者您只需传递id并自己搜索它您要插入哪个实体?
newArticle
听起来好像没有要插入的现有实体。这不是很清楚。有
post
Article
entities,表单管理一个
Post
实体。
Post
是一个新实体还是一个现有实体?NicoHaase,jonas303,Post是我想插入到我的数据库中的一个新实体。一篇文章是一篇文章,但它具有Post实体没有的特定其他字段。事实上,文章继承自Post实体my DB结构。