1

I have a one to many relationship between two tables in a symmfony application. The registration method of one of the table accepts a JSON POST request. On attempting to insert data into the database manually from mysql interface everything is fine but when I attempt to use a POST request to perform the insertion I get

500 Internal Server Error with a Integrity constraint violation:

this is the controller of the POST request

                   $register->setConfirmpassword($params["confirmpassword"]);
                   $register->setAccountfk($params["accountsfk"]);

this is the sample JSON post from postman

{
    "accountsfk":2
    }

registration has a foreign key to accounts. On making a post request I get this error

with params ["myname", "mylastname", "emails123@email.com", "passwords", "passwords", null]:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'accountsfk' cannot be null (500 Internal Server Error)

Please what am doing wrong that I am unable to insert records to the registration table

parker
  • 255
  • 2
  • 6
  • 21

1 Answers1

1

I think that accountsfk is a foreign key so you need to pass the account not the id to the registration, something like this:

$em = $this->getDoctrine()->getManager();       
$account = $em->getRepository('AppBundle:Account')->find($params["accountsfk"]);

$register->setFirstname($params["firstname"]);
$register->setLastname($params["lastname"]);
$register->setEmail($params["email"]);
$register->setPassword($params["password"]);
$register->setConfirmpassword($params["confirmpassword"]);
$register->setAccountfk($params["accountsfk"]);
$register->setAccounts($account]);

In first line I get the account but I don't know which is your bundle and your entity for account, but It's important to understand that you have to pass an entity not the id into setAccountfk

You have setAccountfk that isn't necessary I think, you only need setAccounts to relation registration to account

Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171