Following this guide https://symfony.com/doc/current/form/embedded.html I created a form with three entities. Patient, PatientSample and PatientOrder. When I submit the form I get an error that patient_id can not be null.
I've been trying various ways to create a new Patient object in the PatientOrderController and getting the data there then calling $entityManager->persist($patient) but was having a problem populating the new Patient object with form data.
PatientOrderType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('accession_number')
->add('patient', PatientType::class)
->add('patientSample', PatientSampleType::class);
}
PatientOrderController
{
$patientOrder = new PatientOrder();
$form = $this->createForm(PatientOrderType::class, $patientOrder);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$patientOrder = $form->getData();
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($patientOrder);
$entityManager->flush();
return $this->redirectToRoute('patient_order_index');
}
Patient class
class Patient
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
public $id;
/**
* @ORM\OneToMany(targetEntity="App\Entity\PatientOrder",
* mappedBy="patient")
*/
public $patientOrders;
/**
* @ORM\OneToMany(targetEntity="App\Entity\PatientSample",
* mappedBy="patient")
*/
public $patientSamples;
PatientOrder Class
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Patient",
* inversedBy="patientOrders", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
* @Assert\Valid()
*/
protected $patient;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\PatientSample",
*inversedBy="patientOrders", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
* @Assert\Valid()
*/
private $patientSample;
PatientSample class
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Patient",
* inversedBy="patientSamples")
* @ORM\JoinColumn(nullable=false)
*/
private $patient;
/**
* @ORM\OneToMany(targetEntity="App\Entity\PatientOrder",
* mappedBy="patientSample")
*/
private $patientOrders;
My expectations are that when the user clicks the save button it will insert 3 new rows into 3 separate tables in the database. Instead I'm getting SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'patient_id' cannot be null
Patient,PatientSampleandPatientOrder(only associations, their mappings, fields and setters are relevant here).PatientOrderis persisted. however, you seem to assume that the error is triggered by thePatientOrder'spatient_idfield. your code might suggest, that thePatientSample- if it's actually an entity - might have apatient_idfield as well which might not be connected to a patient yet (hence missing it's value -> null). So I guess, I want to ask, if there's aPatientSampleentity and if so, what are the associations for itPatienthas a OneToMany relationship withPatientSampleso thePatientSampleclass has aPatientfield and vice versa - I'll update the code with that information.PatientOrderandPatientSample? but I already see that my assumption in my last comment is probably the reason...