Symfony 3.4 - Set a many to one API REST

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Symfony 3.4 - Set a many to one API REST



I'm creating an API REST and I want to set for $userInterest an Entity named Category (many to one) I did the same thing for the User entity (and I set the "Profile" entity), but the Profile entity is passed as parameter and it's created in this function so it's simple and it works.


$userInterest


Category


User


Profile



I do it like this:


/**
*
* @RestPost(
* path = "/users/register",
* name = "api_users_add"
* )
* @RestView(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"}}},
* )
* @ParamConverter(
* "userInterest",
* converter="fos_rest.request_body",
* options={"deserializationContent"={"groups"={"Deserialize"}}},
* )
*/
public function postUserAction(Request $request, User $user, Profile
$profile, UserInterest $userInterest) {

$profile->setLastConnexion(new DateTime('now'));
$profile->setCreatedAccount(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);
}



-



So the previus example works really well but when I try to set the Category entity for $userInterest that don't work (also category is already created it's like a "static" table) so the problem is how can I set Category for $userInterest knowing that in the previous example the entity is created in the function and in this example the entity is already created so I tried:


Category


$userInterest


Category


$userInterest


$em_category = $this->getDoctrine()->getManager();
$em_category->getRepository('AppBundle:Category')->findOneBy(array('id' => ($request->get('id_category'))));

$userInterest->setCategory($em_category);

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



But that makes the error:



"An exception occurred while executing 'SELECT t0.content_c AS
content_c_1, t0.id AS id_2 FROM category t0 WHERE t0.id = ? LIMIT 1'
with params ["1"]:



SQLSTATE[42S22]: Column not found: 1054 Unknown column 't0.content_c'
in 'field list'"



The JSON sent (if that can help) :


{
"username": "Usertest",
"password": "strenghtpassword",
"email": "thxforall@symfony.com",
"birth": "1999-04-26T18:25:43-05:00",
"content": "I like Netflix",
"id_category": "1"
}



In category table the ID:1 as the content "what do you like" for example.









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived