无法猜测如何获得Doctrine实例

时间:2017-05-23 13:32:53

标签: php symfony doctrine-orm

在symfony项目中,我试图坚持一个由两个对象(profil和role)组成的关联表(profil_role)。

首先,我以这种方式在第二个项目的ProfilRoleController中开发了创建动作:

/** @var Roles $role */
 $em = $this->getDoctrine()->getManager('main_project');
 $role = $em->getRepository("MyBundle\Entity\Roles")->find($roleId);
 $profil = $em->getRepository("MyBundle\Entity\Profil")->find($profilId);
 $profilRole = new ProfilRoles();
 $profilRole->setRoleId($role->getId());
 $profilRole->setProfilId($profil->getId());
 $em->persist($profilRole);
 $em->flush();

这部分代码,然后调用主项目中的post实体操作:

/**
 * @Rest\View(statusCode=Response::HTTP_CREATED)
 * @Rest\Post("/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注释,但我不知道如何使用它。

2 个答案:

答案 0 :(得分:0)

试试这个:

public function postEntityAction()  {
   $postData = $request->request->all();
   $profileRole = $postData['profile_role']

而不是:

public function postEntityAction(ProfilRoles $profilRole)

答案 1 :(得分:0)

@AlessandroMinoccheri我试图通过你的回复来启发我的工作,我不知道这是否正确。

 /**
 * @param ProfilRoles $profilRole
 * @param Request $request
 * @return ProfilRoles
 * @Rest\View(statusCode=Response::HTTP_CREATED)
 * @Rest\Post("/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;
}