I have created a relationship between two classes in my symfony2 application. It designed in such that tableB is related to tableA I have entered data in the tableA table (as the super entity) and in my controller I am using json to make a post to insert data into the tableB table. The referencing foreign key from tableB takes an integer but when I try to make a post from postman I get this weird error
{
"message": "Expected value of type "\myBundle\Entity\TableA" for association field "\myBundle\Entity\Platforms#$dataValue", got "integer" instead.",
"class": "Doctrine\ORM\ORMInvalidArgumentException",
this is my json format I am posting from postman
{
"id": 6,
"variable": false,
"variable": true,
"variable": true,
"variable": true,
"variable": false,
"variable": true,
"variable": true,
"variable": true,
"variable": false,
"variable": false,
"variable": true,
"fkValue":3 //area of challenge
}
In my entity class I have this code snippet
/**
*
*
* @param \MyBundle\Entity\TableA $dataValue
* @return Platforms
*/
public function setValue(\Api3Bundle\Entity\TableA $dataValue = null)
{
$this->dataValue = $dataValue ;
return $this;
}
================EDITED==================
this is how I am inserting data from the controller into TableB to the foreign key field of TableA
//getting the value from request
$variable = $request->get('variable');
//setting the value for persistence
$data->setValue($variable);//area of challenge
$em = $this->getDoctrine()->getManager();
$em = $this->getDoctrine()->getManager();
$em->persist($data);
I dont know if I am on the right track but can some show me how to insert data into the foreign key field using json from postman. Much thanks
Since you are sending only the key, you can use:
$data->setValue($this->entityManager->getReference('EntityName', $request->get('fkValue')));//area of challenge
Source: http://doctrine-orm.readthedocs.io/en/latest/reference/unitofwork.html