Symfony api rest =>将值设置为多(我认为这很容易回答)

时间:2018-07-25 15:01:33

标签: php rest api symfony post

我有一个简单的问题!我不知道如何在名为profile的表中将$ email的值设置为示例,并且该profile表链接到user表。因此,我成功地使用POST方法添加了用户:

{
"username": "TestTest10",
"password": "TestTest10",
}

之后,我会自动创建一个与用户链接的个人资料实体,但我会手动输入个人资料的信息(例如:

$profile = new Profile();
$profile->setEmail('ok@ok.com');

但是我想使用该POST方法对其进行编辑:

{
"username": "TestTest10",
"password": "TestTest10",
"email": "imanoob@symfony.com"
}

那我该怎么办呢?

我的PostUserFunction:

/**
 *
 * @Rest\Post(
 *     path = "/users",
 *     name = "api_users_add"
 * )
 * @Rest\View(StatusCode=201, serializerGroups={"user_detail"})
 * @ParamConverter(
 *     "user",
 *     converter="fos_rest.request_body",
 *     options={"deserializationContent"={"groups"={"Deserialize"}}}
 * )
 */
public function postUserAction(Request $request, User $user, ConstraintViolationListInterface $violations)
{
    if (count($violations) > 0) {
        return new JsonResponse([
            'success' => "false",
        ]);
    }

    $profile = new Profile();

    $profile->setEmail('ok@ok.com');
    $profile->setLastConnexion(new \DateTime('now'));
    $profile->setCreatedAccount(new \DateTime('now'));
    $profile->setBirth(new \DateTime('now'));

    $user->setIdProfile($profile);

    $em = $this->getDoctrine()->getManager();
    $em->persist($profile);
    $em->flush();

    $this->encodePassword($user);
    $user->setRoles([User::ROLE_USER]);

    $this->persistUser($user);

    return new JsonResponse([
        'success' => "true",
        'data' => [
            'Id' => $user->getId(),
            'username' => $user->getUsername(),
            'email' => $profile->getEmail(),
        ]
    ]);
}

感谢所有尝试回答:p

的人

0 个答案:

没有答案