Doctrine 将DateTime对象解析为原则中的字符串';s查询生成器

Doctrine 将DateTime对象解析为原则中的字符串';s查询生成器,doctrine,symfony4,query-builder,Doctrine,Symfony4,Query Builder,我正在用symfony4框架编写应用程序。我有如下实体: <?php namespace App\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use DateTime; /** * @ORM\Entity(repositoryClass="App\Reposit

我正在用symfony4框架编写应用程序。我有如下实体:

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use DateTime;

/**
 * @ORM\Entity(repositoryClass="App\Repository\CompanyRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class Company
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

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

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $taxNumber;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $street;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $city;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $postalCode;

    /**
     * @ORM\Column(type="integer")
     *
     */
    private $officeId;

    /**
     * @var datetime $created
     *
     * @ORM\Column(name="created_at", type="datetime", nullable = false)
     */
    private $createdAt;

    /**
     * @var datetime $updated
     *
     * @ORM\Column(name="updated_at", type="datetime", nullable = false)
     */
    private $updatedAt;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Employee", mappedBy="company")
     */
    private $employees;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\AuditCompany", mappedBy="company")
     */
    private $companyAudits;

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


    /**
     * Gets triggered only on insert
     * @ORM\PrePersist
     */
    public function onPrePersist()
    {
        $this->createdAt = new DateTime("now");
        $this->updatedAt = new DateTime("now");
    }

    /**
     * Gets triggered every time on update
     * @ORM\PreUpdate
     */
    public function onPreUpdate()
    {
        $this->updatedAt = new DateTime("now");
    }

    public function setId($id): self
    {
        $this->id = $id;

        return $this;
    }


    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(?string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getTaxNumber(): ?string
    {
        return $this->taxNumber;
    }

    public function setTaxNumber(string $taxNumber): self
    {
        $this->taxNumber = $taxNumber;

        return $this;
    }

    public function getStreet(): ?string
    {
        return $this->street;
    }

    public function setStreet(?string $street): self
    {
        $this->street = $street;

        return $this;
    }

    public function getCity(): ?string
    {
        return $this->city;
    }

    public function setCity(string $city): self
    {
        $this->city = $city;

        return $this;
    }

    public function getPostalCode(): ?string
    {
        return $this->postalCode;
    }

    public function setPostalCode(string $postalCode): self
    {
        $this->postalCode = $postalCode;

        return $this;
    }

    public function getOfficeId(): ?string
    {
        return $this->officeId;
    }

    public function setOfficeId(string $officeId): self
    {
        $this->officeId = $officeId;

        return $this;
    }

    public function getCreatedAt(): string
    {
        return $this->createdAt->format('Y-m-d H:i:s O');
    }

    public function setCreatedAt(DateTime $createdAt): self
    {
        $this->createdAt = $createdAt;
        return $this;
    }

    public function getUpdatedAt(): string
    {
        return $this->updatedAt->format('Y-m-d H:i:s O');
    }

    public function setUpdatedAt(DateTime $updatedAt): self
    {
        $this->updatedAt = $updatedAt;
        return $this;
    }

    /**
     * @return Collection|Employee[]
     */
    public function getEmployees(): Collection
    {
        return $this->employees;
    }

    public function addEmployee(Employee $employee): self
    {
        if (!$this->employees->contains($employee)) {
            $this->employees[] = $employee;
            $employee->setCompany($this);
        }

        return $this;
    }

    public function removeEmployee(Employee $employee): self
    {
        if ($this->employees->contains($employee)) {
            $this->employees->removeElement($employee);
            // set the owning side to null (unless already changed)
            if ($employee->getCompany() === $this) {
                $employee->setCompany(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection|AuditCompany[]
     */
    public function getCompanyAudits(): Collection
    {
        return $this->companyAudits;
    }

    public function addCompanyAudit(AuditCompany $companyAudit): self
    {
        if (!$this->companyAudits->contains($companyAudit)) {
            $this->companyAudits[] = $companyAudit;
            $companyAudit->setCompany($this);
        }

        return $this;
    }

    public function removeCompanyAudit(AuditCompany $companyAudit): self
    {
        if ($this->companyAudits->contains($companyAudit)) {
            $this->companyAudits->removeElement($companyAudit);
            // set the owning side to null (unless already changed)
            if ($companyAudit->getCompany() === $this) {
                $companyAudit->setCompany(null);
            }
        }

        return $this;
    }
}
主要问题是createdAt和updatedAt的日期。查询结果如下:

{
    "id": 2,
    "name": "Auto serwis",
    "taxNumber": "PL831231231",
    "street": "Sienkiewicza",
    "city": "Warsaw",
    "postalCode": "55-232",
    "officeId": 11,
    "createdAt": {
        "date": "2019-09-13 10:55:19.000000",
        "timezone_type": 3,
        "timezone": "Europe/Berlin"
    },
    "updatedAt": {
        "date": "-0001-11-30 00:00:00.000000",
        "timezone_type": 3,
        "timezone": "Europe/Berlin"
    }
}
问题是我想获得createdAt和updatedAt的字符串值,而不是像上面的响应中那样的对象。我有createdAt和updatedAt的getter方法,它们返回格式化字符串:

public function getCreatedAt(): string
    {
        return $this->createdAt->format('Y-m-d H:i:s O');
    }
我想以相同的字符串格式获取日期。我该怎么做?如果能得到帮助,我将不胜感激。
致以最诚挚的问候。

尝试getScalarResult()而不是getArrayResult()。结果非常相似:“a_updatedAt”:{“date”:“2019-07-16 14:35:01.000000”,“timezone_type”:3,“timezone”:“Europe/Berlin”},使用
json_encode($repository->find($id))
?请添加此步骤的代码。
public function getCreatedAt(): string
    {
        return $this->createdAt->format('Y-m-d H:i:s O');
    }