代码之家  ›  专栏  ›  技术社区  ›  Element Zero

条令实体总是在持久时保存空值,即使设置了

  •  0
  • Element Zero  · 技术社区  · 6 年前

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

    我的代码:

    $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之前对那个转储进行了双重检查,它正确地设置了字段。然而,当页面运行时…

    sqlstate[23000]:违反完整性约束:1048列 “患者ID”不能为空

    我不明白它怎么可能是空的?有什么想法吗?

    订单实体

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

    编辑:

    我决定只对数据库运行persist输出的SQL语句,这不仅是病人丢失的,而且是主客户机ID字段。我现在假设这与我在那里为病人和主要客户建立的联系有关,因为这两个领域是缺失的。不过,我不确定会出什么问题。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Element Zero    6 年前

    为了子孙后代的利益,在发表评论的讨论之后,这是我的实体的最终结果。现在它将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]