PHP错误:在字符串上调用成员函数format()

时间:2017-09-09 04:43:30

标签: php mysql database doctrine backend

我有一个问题,我不知道问题出在哪里。当我将数据保存到服务器时,我遇到错误调用字符串上的成员函数格式()。

这是我的控制器代码:

    public function newAction(Request $request)
{
    $this->denyAccessUnlessGranted('ROLE_HRM_SALARY_ADJUSTMENT_CREATE');
    $corp = $this->getSelectedCorporationEntity();
    $entity = new SalaryAdjustment();
    $entity->setCorporation($corp);
    if ($request->isMethod('POST')) {
        $data = json_decode($request->getContent());
        dump($data);
        //$this->validationBeforeProgress('new', $data, $data->level, '', $data->docType);
        $employeeEntity =  $this->getDoctrine()->getRepository(Employee::class)->find($data->employee);
        $entity->setEmployee($employeeEntity);
        $entity->setNumber($data->number);
        $entity->setEffectiveDate($data->effectiveDate);
        $entity->setReference($data->reference);
        $entity->setReferenceNumber($data->referenceNumber);
        $entity->setRemarks($data->remarks);
        $entity->setApprove(false);
        $entity->setVoid(false);
        $entity->setCreatedBy($this->getCurrentUser()->getUsername());
        $entity->setCreatedDate(new \DateTime());
        $em = $this->getEM();
        $this->validateEntity($entity);
        $em->persist($entity);

        $newEntityDetail = $data->detail;
        if (count($newEntityDetail) > 0) {
            foreach ($newEntityDetail as $item) {
                dump($item);
                if ($item->newValue > 0 || $item->oldValue > 0){
                    $entityDetail = new SalaryAdjustmentDetail();
                    $lookupSalaryEntity =  $this->getDoctrine()->getRepository(LookupSalary::class)->find($item->lookupSalaryId);
                    $entityDetail->setLookupSalary($lookupSalaryEntity);
                    if ($item->oldEffectiveDate != null) {
                        $entityDetail->setOldEffectiveDate($item->oldEffectiveDate);
                    }

                    $entityDetail->setOldValue($item->oldValue);
                    $entityDetail->setNewValue($item->newValue);
                    $entityDetail->setSalaryAdjustment($entity);
                    $this->validateEntity($entityDetail);
                    $em->persist($entityDetail);
                }
            }
        } else {
            throw new \Exception('Detail must be have minimal 1 row');
        }
        $em->flush();
    }
    return new JsonResponse(['data' => 'Done']);
}

错误来自代码$ em-> flush()。我觉得问题来自代码$ entityDetail-> setOldValue($ item-> oldValue);和entityDetail-> setNewValue($ item-> newValue);因为我第一次在实体中使用十进制类型。

我使用2个实体来保存我的数据,实体是SalaryAdjustment和SalaryAdjustmentDetail。有代码:

class SalaryAdjustment
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\OneToMany(targetEntity="SalaryAdjustmentDetail", mappedBy="salaryAdjustment", cascade={"persist", "remove"})
     */
    private $salaryAdjustmentDetail;

    public function __construct() {
        $this->salaryAdjustmentDetail = new ArrayCollection();
    }

    /**
     * @ORM\ManyToOne(targetEntity="Dsp\DspAppsBundle\Entity\HRM\Employee\Employee")
     * @ORM\JoinColumn(name="employee_id", referencedColumnName="id", nullable=false)
     */
    private $employee;

    /**
     * @ORM\ManyToOne(targetEntity="Dsp\DspAdministrationBundle\Entity\Corporation")
     * @ORM\JoinColumn(name="corporation_id", referencedColumnName="id", nullable=false)
     */
    private $corporation;

    /**
     * @var string
     *
     * @ORM\Column(name="number", type="string", length=20)
     */
    private $number;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="effective_date", type="date")
     */
    private $effectiveDate;

    /**
     * @var string
     *
     * @ORM\Column(name="reference", type="string", length=50, nullable=true)
     */
    private $reference;

    /**
     * @var string
     *
     * @ORM\Column(name="reference_number", type="string", length=100, nullable=true)
     */
    private $referenceNumber;

    /**
     * @var string
     *
     * @ORM\Column(name="remarks", type="string", length=300, nullable=true)
     */
    private $remarks;

    /**
     * @var bool
     *
     * @ORM\Column(name="approve", type="boolean")
     */
    private $approve;

    /**
     * @var bool
     *
     * @ORM\Column(name="void", type="boolean")
     */
    private $void;

    /**
     * @var string
     *
     * @ORM\Column(name="created_by", type="string", length=50)
     */
    private $createdBy;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created_date", type="datetime")
     */
    private $createdDate;

    /**
     * @var string
     *
     * @ORM\Column(name="modified_by", type="string", length=50, nullable=true)
     */
    private $modifiedBy;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="modified_date", type="datetime", nullable=true)
     */
    private $modifiedDate;


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Add salaryAdjustmentDetail
     *
     * @param \Dsp\DspAppsBundle\Entity\HRM\SalaryAdjustment\SalaryAdjustmentDetail $salaryAdjustmentDetail
     *
     * @return SalaryAdjustment
     */
    public function addSalaryAdjustmentDetail(SalaryAdjustmentDetail $salaryAdjustmentDetail)
    {
        $this->salaryAdjustmentDetail[] = $salaryAdjustmentDetail;

        return $this;
    }

    /**
     * Remove salaryAdjustmentDetail
     *
     * @param \Dsp\DspAppsBundle\Entity\HRM\SalaryAdjustment\SalaryAdjustmentDetail $salaryAdjustmentDetail
     */
    public function removeSalaryAdjustmentDetail(\Dsp\DspAppsBundle\Entity\HRM\SalaryAdjustment\SalaryAdjustmentDetail $salaryAdjustmentDetail)
    {
        $this->salaryAdjustmentDetail->removeElement($salaryAdjustmentDetail);
    }

    /**
     * Set employee
     *
     * @param \Dsp\DspAppsBundle\Entity\HRM\Employee\Employee $employee
     *
     * @return SalaryAdjustment
     */
    public function setEmployee(\Dsp\DspAppsBundle\Entity\HRM\Employee\Employee $employee)
    {
        $this->employee = $employee;

        return $this;
    }

    /**
     * Get employee
     *
     * @return \Dsp\DspAppsBundle\Entity\HRM\Employee\Employee
     */
    public function getEmployee()
    {
        return $this->employee;
    }

    /**
     * Set corporation
     *
     * @param \Dsp\DspAdministrationBundle\Entity\Corporation $corporation
     *
     * @return SalaryAdjustment
     */
    public function setCorporation(\Dsp\DspAdministrationBundle\Entity\Corporation $corporation)
    {
        $this->corporation = $corporation;

        return $this;
    }

    /**
     * Get corporation
     *
     * @return \Dsp\DspAdministrationBundle\Entity\Corporation
     */
    public function getCorporation()
    {
        return $this->corporation;
    }

    /**
     * Set number
     *
     * @param string $number
     *
     * @return SalaryAdjustment
     */
    public function setNumber($number)
    {
        $this->number = $number;

        return $this;
    }

    /**
     * Get number
     *
     * @return string
     */
    public function getNumber()
    {
        return $this->number;
    }

    /**
     * Set effectiveDate
     *
     * @param \DateTime $effectiveDate
     *
     * @return SalaryAdjustment
     */
    public function setEffectiveDate($effectiveDate)
    {
        $this->effectiveDate = $effectiveDate;

        return $this;
    }

    /**
     * Get effectiveDate
     *
     * @return \DateTime
     */
    public function getEffectiveDate()
    {
        return $this->effectiveDate;
    }

    /**
     * Set reference
     *
     * @param string $reference
     *
     * @return SalaryAdjustment
     */
    public function setReference($reference)
    {
        $this->reference = $reference;

        return $this;
    }

    /**
     * Get reference
     *
     * @return string
     */
    public function getReference()
    {
        return $this->reference;
    }

    /**
     * Set referenceNumber
     *
     * @param string $referenceNumber
     *
     * @return SalaryAdjustment
     */
    public function setReferenceNumber($referenceNumber)
    {
        $this->referenceNumber = $referenceNumber;

        return $this;
    }

    /**
     * Get referenceNumber
     *
     * @return string
     */
    public function getReferenceNumber()
    {
        return $this->referenceNumber;
    }

    /**
     * Set remarks
     *
     * @param string $remarks
     *
     * @return SalaryAdjustment
     */
    public function setRemarks($remarks)
    {
        $this->remarks = $remarks;

        return $this;
    }

    /**
     * Get remarks
     *
     * @return string
     */
    public function getRemarks()
    {
        return $this->remarks;
    }

    /**
     * Set approve
     *
     * @param boolean $approve
     *
     * @return SalaryAdjustment
     */
    public function setApprove($approve)
    {
        $this->approve = $approve;

        return $this;
    }

    /**
     * Get approve
     *
     * @return bool
     */
    public function getApprove()
    {
        return $this->approve;
    }

    /**
     * Set void
     *
     * @param boolean $void
     *
     * @return SalaryAdjustment
     */
    public function setVoid($void)
    {
        $this->void = $void;

        return $this;
    }

    /**
     * Get void
     *
     * @return bool
     */
    public function getVoid()
    {
        return $this->void;
    }

    /**
     * Set createdBy
     *
     * @param string $createdBy
     *
     * @return SalaryAdjustment
     */
    public function setCreatedBy($createdBy)
    {
        $this->createdBy = $createdBy;

        return $this;
    }

    /**
     * Get createdBy
     *
     * @return string
     */
    public function getCreatedBy()
    {
        return $this->createdBy;
    }

    /**
     * Set createdDate
     *
     * @param \DateTime $createdDate
     *
     * @return SalaryAdjustment
     */
    public function setCreatedDate($createdDate)
    {
        $this->createdDate = $createdDate;

        return $this;
    }

    /**
     * Get createdDate
     *
     * @return \DateTime
     */
    public function getCreatedDate()
    {
        return $this->createdDate;
    }

    /**
     * Set modifiedBy
     *
     * @param string $modifiedBy
     *
     * @return SalaryAdjustment
     */
    public function setModifiedBy($modifiedBy)
    {
        $this->modifiedBy = $modifiedBy;

        return $this;
    }

    /**
     * Get modifiedBy
     *
     * @return string
     */
    public function getModifiedBy()
    {
        return $this->modifiedBy;
    }

    /**
     * Set modifiedDate
     *
     * @param \DateTime $modifiedDate
     *
     * @return SalaryAdjustment
     */
    public function setModifiedDate($modifiedDate)
    {
        $this->modifiedDate = $modifiedDate;

        return $this;
    }

    /**
     * Get modifiedDate
     *
     * @return \DateTime
     */
    public function getModifiedDate()
    {
        return $this->modifiedDate;
    }
}

这是我的SalaryAdjustmentDetail:

    class SalaryAdjustmentDetail
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Dsp\DspAppsBundle\Entity\HRM\Lookup\LookupSalary")
     * @ORM\JoinColumn(name="lookup_salary_id", referencedColumnName="id", nullable=false)
     */
    private $lookupSalary;

    /**
     * @ORM\ManyToOne(targetEntity="Dsp\DspAppsBundle\Entity\HRM\SalaryAdjustment\SalaryAdjustment", inversedBy="SalaryAdjustmentDetail")
     * @ORM\JoinColumn(name="salary_adjustment_id", referencedColumnName="id", onDelete="CASCADE", nullable=false)
     */
    private $salaryAdjustment;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="old_effective_date", type="date", nullable=true)
     */
    private $oldEffectiveDate;

    /**
     * @var string
     *
     * @ORM\Column(name="old_value", type="decimal", precision=20, scale=4)
     */
    private $oldValue;

    /**
     * @var string
     *
     * @ORM\Column(name="new_value", type="decimal", precision=20, scale=4)
     */
    private $newValue;


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set lookupSalary
     *
     * @param \Dsp\DspAppsBundle\Entity\HRM\Lookup\LookupSalary $lookupSalary
     *
     * @return SalaryAdjustmentDetail
     */
    public function setLookupSalary(\Dsp\DspAppsBundle\Entity\HRM\Lookup\LookupSalary $lookupSalary)
    {
        $this->lookupSalary = $lookupSalary;

        return $this;
    }

    /**
     * Get lookupSalary
     *
     * @return \Dsp\DspAppsBundle\Entity\HRM\Lookup\LookupSalary
     */
    public function getLookupSalary()
    {
        return $this->lookupSalary;
    }

    /**
     * Set salaryAdjustment
     *
     * @param \Dsp\DspAppsBundle\Entity\HRM\SalaryAdjustment\SalaryAdjustment $salaryAdjustment
     *
     * @return SalaryAdjustmentDetail
     */
    public function setSalaryAdjustment(\Dsp\DspAppsBundle\Entity\HRM\SalaryAdjustment\SalaryAdjustment $salaryAdjustment)
    {
        $this->salaryAdjustment = $salaryAdjustment;

        return $this;
    }

    /**
     * Get salaryAdjustment
     *
     * @return \Dsp\DspAppsBundle\Entity\HRM\SalaryAdjustment\SalaryAdjustment
     */
    public function getSalaryAdjustment()
    {
        return $this->salaryAdjustment;
    }

    /**
     * Set oldEffectiveDate
     *
     * @param \DateTime $oldEffectiveDate
     *
     * @return SalaryAdjustmentDetail
     */
    public function setOldEffectiveDate($oldEffectiveDate)
    {
        $this->oldEffectiveDate = $oldEffectiveDate;

        return $this;
    }

    /**
     * Get oldEffectiveDate
     *
     * @return \DateTime
     */
    public function getOldEffectiveDate()
    {
        return $this->oldEffectiveDate;
    }

    /**
     * Set oldValue
     *
     * @param string $oldValue
     *
     * @return SalaryAdjustmentDetail
     */
    public function setOldValue($oldValue)
    {
        $this->oldValue = $oldValue;

        return $this;
    }

    /**
     * Get oldValue
     *
     * @return string
     */
    public function getOldValue()
    {
        return $this->oldValue;
    }

    /**
     * Set newValue
     *
     * @param string $newValue
     *
     * @return SalaryAdjustmentDetail
     */
    public function setNewValue($newValue)
    {
        $this->newValue = $newValue;

        return $this;
    }

    /**
     * Get newValue
     *
     * @return string
     */
    public function getNewValue()
    {
        return $this->newValue;
    }
}

请帮我解决我的问题:')

0 个答案:

没有答案