在symfony项目中,我试图保留由两个对象(profil和role(组成的关联表(profil_role(的一行。
首先,我以这种方式开发了第二个项目的ProfilRoleController
中的创建操作:
/** @var Roles $role */
$em = $this->getDoctrine()->getManager('main_project');
$role = $em->getRepository("MyBundleEntityRoles")->find($roleId);
$profil = $em->getRepository("MyBundleEntityProfil")->find($profilId);
$profilRole = new ProfilRoles();
$profilRole->setRoleId($role->getId());
$profilRole->setProfilId($profil->getId());
$em->persist($profilRole);
$em->flush();
这部分代码,然后调用主项目中存在的 post 实体操作:
/**
* @RestView(statusCode=Response::HTTP_CREATED)
* @RestPost("/profil_roles")
*/
public function postEntityAction(ProfilRoles $profilRole)
{
$em = $this->getDoctrine()->getManager();
$em->persist($profilRole);
$em->flush();
return $profilRole;
}
当我尝试执行我的代码时,我得到了这个错误之王:
Execution failed for request: POST /api/profil_roles? HTTP/1.1 {"profil":{"id":"12"},"role":{"id":"3"}}: HTTPCode 500, body {"code":500,"message":"Unable to guess how to get a Doctrine instance from the request information."}
我尝试使用@ParamConverter
注释,但我不知道如何使用它。
试试这个:
public function postEntityAction() {
$postData = $request->request->all();
$profileRole = $postData['profile_role']
取而代之的是:
public function postEntityAction(ProfilRoles $profilRole)
@AlessandroMinoccheri我
试图从你的回复中得到启发来做这件事,但我没有工作,我不知道这是否是正确的方法。
/**
* @param ProfilRoles $profilRole
* @param Request $request
* @return ProfilRoles
* @RestView(statusCode=Response::HTTP_CREATED)
* @RestPost("/profil_roles")
*/
public function postEntityAction(Request $request)
{
$profilRole = new ProfilRoles();
$em = $this->getDoctrine()->getManager();
$requete = $request->request->all();
$profilRole->setProfilId($requete['profil']['id']);
$profilRole->setRoleId($requete['role']['id']);
$em->persist($profilRole);
$em->flush();
return $profilRole;
}