как я могу получить только дату из объекта dateTime? - PullRequest
1 голос
/ 06 мая 2020

Когда я хочу получить дату столбца, он показывает мне это . вот мой код для объекта, который содержит атрибут dateTime:

/**
 * @ORM\Column(type="datetime")
 */
private $dateEcheance;

public function getDateEcheance(): ?\DateTimeInterface
{
    $this->dateEcheance->format('d/m/Y');
    return $this->dateEcheance;
}

public function setDateEcheance(\DateTimeInterface $dateEcheance): self
{
    date_default_timezone_set('Africa/Tunis');
    $this->dateEcheance = $dateEcheance;

    return $this;
}

, и это мой API:

/**
 * @Route("/api/getContract", name="getContract", methods={"GET"})
*/
public function getContract(ContratRepository $contratRepo) 
{    
    $contrat= $contratRepo->findAll();

    $encoders = [new JsonEncoder()];
    $normalizers =[new ObjectNormalizer()];
    $serializer =new Serializer($normalizers, $encoders);
    $jsonContent =$serializer->serialize($contrat, 'json', ['circular_reference_handler' => function($object){
        return $object->getId();
    }]);

    $response = new Response($jsonContent);

    $response->headers->set('Content-Type', 'application/json');
    $response->headers->set('Access-Control-Allow-Origin', '*');

    return $response;
}

любая помощь будет приложена :)

1 Ответ

0 голосов
/ 06 мая 2020

Ваш метод getDateEcheance возвращает объект DateTime и $ this-> dateEcheance-> format ('d / m / Y'); здесь не уместно.

Возможно, вам нужно создать другой такой метод:

public function getDateEcheanceLibelle(): ?String
{
    if( isset($this->dateEcheance) )
        return $this->dateEcheance->format('d/m/Y');

    return '';
}
...