A beginner
A beginner

Reputation: 124

Symfony 3.4 - Api Rest modify value

I have a simple problem ! I don't know how to set a value to $email that is in a table named profile and that profile table is linked to user table (many to one). So i success to add a user with the POST method :

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

After that, i create automatically a profile entity linked to user but i put the information of profile manually , so it creates the user that i want but the profile linked to this account is always the same for any user (all have the email [email protected]) (like this :

$profile = new Profile();
$profile->setEmail('[email protected]');
$user->setIdProfile($profile);
$em = $this->getDoctrine()->getManager();
$em->persist($profile);
$em->flush();

But i want to edit it with that POST method :

{
"username": "TestTest10",
"password": "TestTest10",
"email": "[email protected]"
}

So how can i do it ?

My 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('[email protected]');
    $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(),
        ]
    ]);
}

I can maybe receive the profile in parameter :

    public function postUserAction(Request $request, User $user, Profile $profile, ConstraintViolationListInterface $violations)

But I don't know how to put two paramConverter in :

* @ParamConverter(
 *     "user",
 *     converter="fos_rest.request_body",
 *     options={"deserializationContent"={"groups"={"Deserialize"}}},
 * )

Thx for all that will try to answer :p

Upvotes: 1

Views: 204

Answers (1)

A beginner
A beginner

Reputation: 124

Ok guys if someone as the same proble the response is really easy but you need to now it : you need to put two param converter like this :

* @Rest\View(StatusCode=201, serializerGroups={"user_detail"})
 * @ParamConverter(
 *     "user",
 *     converter="fos_rest.request_body",
 *     options={"deserializationContent"={"groups"={"Deserialize"}}},
 * )
 * @ParamConverter(
 *     "profile",
 *     converter="fos_rest.request_body",
 *     options={"deserializationContent"={"groups"={"Deserialize"}}},
 * )

The entire code is look like this :

/**
 *
 * @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"}}},
 * )
 * @ParamConverter(
 *     "profile",
 *     converter="fos_rest.request_body",
 *     options={"deserializationContent"={"groups"={"Deserialize"}}},
 * )
 */
public function postUserAction(Request $request, User $user, Profile $profile, ConstraintViolationListInterface $violations)
{
    if (count($violations) > 0) {
        return new JsonResponse([
            'success' => "false",
        ]);
    }
    $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(),
        ]
    ]);
}

Upvotes: 1

Related Questions