I'm trying to persist some data to the database but even though I'm setting a field it sets it to null when the actual SQL runs. The field is set to not null in the database which throws an error.
My code:
$db_order = new Orders;
$db_order->setDateCreated(new \DateTime());
$db_order->setLastUpdated(null);
$db_order->setDateAssigned(null);
$db_order->setCreatedBy($user->getId());
$db_order->setAttachmentId($order->getFaxId());
$db_order->setPatientId($order->getPatientId());
$db_order->setPaymentType($order->getPaymentType());
$db_order->setInsuranceName($order->getInsuranceName());
$db_order->setInsuranceNo($order->getInsuranceNo());
$db_order->setCash($order->getCash());
$db_order->setPrimaryClientId($order->getPrimaryClient());
$db_order->setSecondaryClients($secondaryClients);
$db_order->setEmployees($employees);
$db_order->setDates($dates);
$db_order->setNotes($note);
dump($db_order->getPatientId());
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($db_order);
$entityManager->flush();
I was double checking with that dump before the persist and it has the field set correctly. Yet when the page runs...
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'patient_id' cannot be null
I don't understand how it could be null? Any ideas?
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\PersistentCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\GroupSequenceProviderInterface;
/**
* @ORM\Entity(repositoryClass="App\Repository\OrdersRepository")
* @Assert\GroupSequenceProvider()
*/
class Orders implements GroupSequenceProviderInterface
{
const TYPE_InsuranceOnly = 1;
const TYPE_InsuranceAndCash = 2;
const TYPE_CashOnly = 3;
/**
* @ORM\ManyToOne(targetEntity="Patients")
* @ORM\JoinColumn(name="patient_id", referencedColumnName="id")
*/
private $patients;
/**
* @ORM\ManyToOne(targetEntity="Clients")
* @ORM\JoinColumn(name="primary_client_id", referencedColumnName="id")
*/
private $primaryClient;
public function __construct()
{
}
public function getPatients(): Patients
{
return $this->patients;
}
public function getPrimaryClient(): Clients
{
return $this->primaryClient;
}
/**
* @ORM\Id()
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="datetime")
*/
private $date_created;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $last_updated;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $date_assigned;
/**
* @ORM\Column(type="smallint")
* @ORM\OneToOne(targetEntity="Employee")
* @ORM\JoinColumn(name="created_by", referencedColumnName="id")
*/
private $created_by;
/**
* @ORM\Column(type="integer")
* @ORM\OneToOne(targetEntity="Attachments")
* @ORM\JoinColumn(name="attachment_id", referencedColumnName="id")
*/
private $attachment_id;
/**
* @ORM\Column(type="integer", nullable=false)
*/
private $patient_id;
/**
* @ORM\Column(type="smallint")
* @Assert\NotBlank(groups = {"insuranceAndCash","cashOnly"}, message="If Payment Type is set to Insurance And Collect or Cash Only then Cash field cannot be blank")
*/
private $payment_type;
/**
* @ORM\Column(type="string", length=50, nullable=true)
* @Assert\NotBlank(groups = {"insuranceAndCash","insuranceOnly"}, message="If Payment Type is set to Insurance Only or Insurance And Collect then Insurance Name field cannot be blank")
*/
private $insurance_name;
/**
* @ORM\Column(type="string", length=50, nullable=true)
* @Assert\NotBlank(groups = {"insuranceAndCash","insuranceOnly"}, message="If Payment Type is set to Insurance Only or Insurance And Collect then Insurance # field cannot be blank")
*/
private $insurance_no;
/**
* @ORM\Column(type="decimal", precision=15, scale=2, nullable=true)
* @Assert\NotBlank(groups = {"insuranceAndCash","cashOnly"}, message="If Payment Type is set to Insurance And Collect or Cash Only then Cash field cannot be blank")
*/
private $cash;
/**
* @ORM\Column(name="primary_client_id", type="integer")
*/
private $primaryClientId;
/**
* One Order has Many Secondary Clients.
* @ORM\ManyToMany(targetEntity="Clients")
* @ORM\JoinTable(name="orders_secondary_clients",
* joinColumns={@ORM\JoinColumn(name="order_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="client_id", referencedColumnName="id")}
* )
*/
private $secondaryClients;
/**
* One Order has Many Employees.
* @ORM\ManyToMany(targetEntity="Employee")
* @ORM\JoinTable(name="orders_employees",
* joinColumns={@ORM\JoinColumn(name="order_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="employee_id", referencedColumnName="id")},
* )
*/
private $employees;
/**
* @ORM\OneToMany(targetEntity="OrderDates", mappedBy="order", cascade={"persist", "remove"}, orphanRemoval=TRUE)
*/
private $dates;
/**
* @ORM\Column(type="text", length=100000, nullable=true)
*/
private $notes;
/**
* @ORM\Column(type="smallint")
*/
private $status = 0;
public function getId(): ?int
{
return $this->id;
}
public function getDateAssigned(): ?\DateTimeInterface
{
return $this->date_assigned;
}
public function setDateAssigned(?\DateTimeInterface $date_assigned): self
{
$this->date_assigned = $date_assigned;
return $this;
}
public function getCreatedBy(): ?int
{
return $this->created_by;
}
public function setCreatedBy(int $created_by): self
{
$this->created_by = $created_by;
return $this;
}
public function getDateCreated(): \DateTimeInterface
{
return $this->date_created;
}
public function setDateCreated(\DateTimeInterface $dateCreated): self
{
$this->date_created = $dateCreated;
return $this;
}
public function getLastUpdated(): ?\DateTimeInterface
{
return $this->last_updated;
}
public function setLastUpdated(?\DateTimeInterface $last_updated): self
{
$this->last_updated = $last_updated;
return $this;
}
public function getPatientId(): ?int
{
return $this->patient_id;
}
public function setPatientId(int $patient_id): self
{
$this->patient_id = $patient_id;
return $this;
}
public function getPaymentType(): ?int
{
return $this->payment_type;
}
public function setPaymentType(int $payment_type): self
{
$this->payment_type = $payment_type;
return $this;
}
public function getInsuranceName(): ?string
{
return $this->insurance_name;
}
public function setInsuranceName(?string $insurance_name): self
{
$this->insurance_name = $insurance_name;
return $this;
}
public function getInsuranceNo(): ?string
{
return $this->insurance_no;
}
public function setInsuranceNo(?string $insurance_no): self
{
$this->insurance_no = $insurance_no;
return $this;
}
public function getCash() : float
{
return $this->cash;
}
public function setCash(float $cash): self
{
$this->cash = $cash;
return $this;
}
public function getAttachmentId(): ?int
{
return $this->attachment_id;
}
public function setAttachmentId(int $attachment_id): self
{
$this->attachment_id = $attachment_id;
return $this;
}
public function getPrimaryClientId(): int
{
return $this->primaryClientId;
}
public function setPrimaryClientId(int $client): self
{
$this->primaryClientId = $client;
return $this;
}
public function getSecondaryClients() : ?PersistentCollection
{
return $this->secondaryClients;
}
public function setSecondaryClients(?array $clients): self
{
$this->secondaryClients = $clients;
return $this;
}
public function getEmployees() : PersistentCollection
{
return $this->employees;
}
public function setEmployees(array $employees): self
{
$this->employees = $employees;
return $this;
}
public function getDates() : PersistentCollection
{
return $this->dates;
}
public function addDate(\DateTime $date): self
{
$orderDate = new OrderDates();
$orderDate->setDate($date);
$orderDate->setOrder($this);
$this->dates[] = $orderDate;
return $this;
}
public function setDates(array $dates): self
{
foreach($dates as $date)
{
$this->addDate(new \DateTime($date));
}
return $this;
}
public function getNotes() : ?string
{
return $this->notes;
}
public function setNotes(?string $notes): self
{
$this->notes = $notes;
return $this;
}
public function getStatus() : int
{
return $this->status;
}
public function setStatus(int $status): self
{
$this->notes = $status;
return $this;
}
public function getGroupSequence()
{
$group = null;
switch($this->payment_type) {
case self::TYPE_InsuranceOnly:
$group = 'insuranceOnly';
break;
case self::TYPE_InsuranceAndCash:
$group = 'insuranceAndCash';
break;
case self::TYPE_CashOnly:
$group = 'cashOnly';
break;
}
return [
[
'Orders',
$group
]
];
}
}
I decided to just run the SQL statement output by the persist against the DB and it seems not only was the patient missing but also the primary_client_id field. I now assume it has something to do with the associations I have in there for patients and primaryclient since those are the two fields that are missing. I'm not sure what would be wrong there though.
For the sake of posterity, after the discussion in the post comments this is the end result of my entity. It now saves the id's in the table, there was no need for a separate field JUST to save the int. The only big difference is I need to lookup the patient and primary Client to pass in, instead of just passing the id value (integer).
Code:
$patient = $this->getDoctrine()
->getRepository(Patients::class)
->find($order->getPatientId());
$primaryClient = $this->getDoctrine()
->getRepository(Clients::class)
->findOneById($order->getPrimaryClient());
$db_order = new Orders();
$db_order->setDateCreated(new \DateTime());
$db_order->setLastUpdated(null);
$db_order->setDateAssigned(null);
$db_order->setCreatedBy($user->getId());
$db_order->setAttachmentId($order->getFaxId());
$db_order->setPatient($patient);
$db_order->setPaymentType($order->getPaymentType());
$db_order->setInsuranceName($order->getInsuranceName());
$db_order->setInsuranceNo($order->getInsuranceNo());
$db_order->setCash($order->getCash());
$db_order->setPrimaryClient($primaryClient);
$db_order->setSecondaryClients($secondaryClients);
$db_order->setEmployees($employees);
$db_order->setDates($dates);
$db_order->setNotes($note);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($db_order);
$entityManager->flush();
Entity:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\PersistentCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\GroupSequenceProviderInterface;
/**
* @ORM\Entity(repositoryClass="App\Repository\OrdersRepository")
* @Assert\GroupSequenceProvider()
*/
class Orders implements GroupSequenceProviderInterface
{
const TYPE_InsuranceOnly = 1;
const TYPE_InsuranceAndCash = 2;
const TYPE_CashOnly = 3;
/**
* @ORM\Id()
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="datetime")
*/
private $date_created;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $last_updated;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $date_assigned;
/**
* @ORM\Column(type="smallint")
* @ORM\OneToOne(targetEntity="Employee")
* @ORM\JoinColumn(name="created_by", referencedColumnName="id")
*/
private $created_by;
/**
* @ORM\Column(type="integer")
* @ORM\OneToOne(targetEntity="Attachments")
* @ORM\JoinColumn(name="attachment_id", referencedColumnName="id")
*/
private $attachment_id;
/**
* @ORM\Column(type="smallint")
* @Assert\NotBlank(groups = {"insuranceAndCash","cashOnly"}, message="If Payment Type is set to Insurance And Collect or Cash Only then Cash field cannot be blank")
*/
private $payment_type;
/**
* @ORM\Column(type="string", length=50, nullable=true)
* @Assert\NotBlank(groups = {"insuranceAndCash","insuranceOnly"}, message="If Payment Type is set to Insurance Only or Insurance And Collect then Insurance Name field cannot be blank")
*/
private $insurance_name;
/**
* @ORM\Column(type="string", length=50, nullable=true)
* @Assert\NotBlank(groups = {"insuranceAndCash","insuranceOnly"}, message="If Payment Type is set to Insurance Only or Insurance And Collect then Insurance # field cannot be blank")
*/
private $insurance_no;
/**
* @ORM\Column(type="decimal", precision=15, scale=2, nullable=true)
* @Assert\NotBlank(groups = {"insuranceAndCash","cashOnly"}, message="If Payment Type is set to Insurance And Collect or Cash Only then Cash field cannot be blank")
*/
private $cash;
/**
* One Order has Many Secondary Clients.
* @ORM\ManyToMany(targetEntity="Clients")
* @ORM\JoinTable(name="orders_secondary_clients",
* joinColumns={@ORM\JoinColumn(name="order_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="client_id", referencedColumnName="id")}
* )
*/
private $secondaryClients;
/**
* One Order has Many Employees.
* @ORM\ManyToMany(targetEntity="Employee")
* @ORM\JoinTable(name="orders_employees",
* joinColumns={@ORM\JoinColumn(name="order_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="employee_id", referencedColumnName="id")},
* )
*/
private $employees;
/**
* @ORM\OneToMany(targetEntity="OrderDates", mappedBy="order", cascade={"persist", "remove"}, orphanRemoval=TRUE)
*/
private $dates;
/**
* @ORM\Column(type="text", length=100000, nullable=true)
*/
private $notes;
/**
* @ORM\Column(type="smallint")
*/
private $status = 0;
/**
* @ORM\ManyToOne(targetEntity="Patients")
* @ORM\JoinColumn(name="patient_id", referencedColumnName="id")
*/
private $patient;
/**
* @ORM\ManyToOne(targetEntity="Clients")
* @ORM\JoinColumn(name="primary_client_id", referencedColumnName="id")
*/
private $primaryClient;
public function getId(): ?int
{
return $this->id;
}
public function getDateAssigned(): ?\DateTimeInterface
{
return $this->date_assigned;
}
public function setDateAssigned(?\DateTimeInterface $date_assigned): self
{
$this->date_assigned = $date_assigned;
return $this;
}
public function getCreatedBy(): ?int
{
return $this->created_by;
}
public function setCreatedBy(int $created_by): self
{
$this->created_by = $created_by;
return $this;
}
public function getDateCreated(): \DateTimeInterface
{
return $this->date_created;
}
public function setDateCreated(\DateTimeInterface $dateCreated): self
{
$this->date_created = $dateCreated;
return $this;
}
public function getLastUpdated(): ?\DateTimeInterface
{
return $this->last_updated;
}
public function setLastUpdated(?\DateTimeInterface $last_updated): self
{
$this->last_updated = $last_updated;
return $this;
}
public function getPaymentType(): ?int
{
return $this->payment_type;
}
public function setPaymentType(int $payment_type): self
{
$this->payment_type = $payment_type;
return $this;
}
public function getInsuranceName(): ?string
{
return $this->insurance_name;
}
public function setInsuranceName(?string $insurance_name): self
{
$this->insurance_name = $insurance_name;
return $this;
}
public function getInsuranceNo(): ?string
{
return $this->insurance_no;
}
public function setInsuranceNo(?string $insurance_no): self
{
$this->insurance_no = $insurance_no;
return $this;
}
public function getCash() : float
{
return $this->cash;
}
public function setCash(float $cash): self
{
$this->cash = $cash;
return $this;
}
public function getAttachmentId(): ?int
{
return $this->attachment_id;
}
public function setAttachmentId(int $attachment_id): self
{
$this->attachment_id = $attachment_id;
return $this;
}
public function getSecondaryClients() : ?PersistentCollection
{
return $this->secondaryClients;
}
public function setSecondaryClients(?array $clients): self
{
$this->secondaryClients = $clients;
return $this;
}
public function getEmployees() : PersistentCollection
{
return $this->employees;
}
public function setEmployees(array $employees): self
{
$this->employees = $employees;
return $this;
}
public function getDates() : PersistentCollection
{
return $this->dates;
}
public function addDate(\DateTime $date): self
{
$orderDate = new OrderDates();
$orderDate->setDate($date);
$orderDate->setOrder($this);
$this->dates[] = $orderDate;
return $this;
}
public function setDates(array $dates): self
{
foreach($dates as $date)
{
$this->addDate(new \DateTime($date));
}
return $this;
}
public function getNotes() : ?string
{
return $this->notes;
}
public function setNotes(?string $notes): self
{
$this->notes = $notes;
return $this;
}
public function getStatus() : int
{
return $this->status;
}
public function setStatus(int $status): self
{
$this->notes = $status;
return $this;
}
public function getGroupSequence()
{
$group = null;
switch($this->payment_type) {
case self::TYPE_InsuranceOnly:
$group = 'insuranceOnly';
break;
case self::TYPE_InsuranceAndCash:
$group = 'insuranceAndCash';
break;
case self::TYPE_CashOnly:
$group = 'cashOnly';
break;
}
return [
[
'Orders',
$group
]
];
}
public function getPatient(): Patients
{
return $this->patient;
}
public function setPatient(Patients $patient) : self
{
$this->patient = $patient;
return $this;
}
public function getPrimaryClient(): Clients
{
return $this->primaryClient;
}
pu]