原则实体即使在设置时也始终在保存时保留null

时间:2018-10-12 13:58:08

标签: php symfony doctrine symfony4

我试图将一些数据持久化到数据库中,但是即使我设置了一个字段,它也会在实际SQL运行时将其设置为null。在数据库中将该字段设置为非null会引发错误。

我的代码:

library(stringi)
library(docxtractr)

stri_read_lines("23.txt") %>% 
  stri_split_fixed("\t", simplify = TRUE) %>% 
  as.data.frame(stringsAsFactors=FALSE) %>% 
  docxtractr::assign_colnames(1)
##      old      new
## 1  MAKFA    МАКФА
## 2  makar макароны
## 3 макар. макароны

在持久化之前,我已经对该转储进行了仔细检查,并且其字段设置正确。但是,当页面运行时...

  

SQLSTATE [23000]:违反完整性约束:1048列   “ patient_id”不能为空

我不知道它怎么可能为空?有什么想法吗?

订单实体

$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();

编辑:

我决定只对数据库运行由persist生成的SQL语句输出,看来不仅是患者失踪,而且primary_client_id字段也是如此。现在,我认为这与我在那里为患者和主要客户建立的联系有关,因为这是两个缺少的领域。我不确定那里到底有什么问题。

1 个答案:

答案 0 :(得分:0)

为了后代,在帖子中的讨论之后,这是我实体的最终结果。现在,它将id保存在表中,无需单独的字段即可保存int。唯一的不同是,我需要查找要传入的患者和主要客户,而不是仅传递id值(整数)。

代码:

$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();

实体:

<?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]