docs icon indicating copy to clipboard operation
docs copied to clipboard

DateTimeImmutable $publicationDate should be DateTime

Open dalehurley opened this issue 3 years ago • 2 comments

After finishing Getting Started With API Platform: Create Your API and Your Jamstack Site and moving onto Testing the API when you run

docker-compose exec php \
    bin/console hautelook:fixtures:load

You get an error

In SimpleObjectGenerator.php line 111:
                                                                                  
  An error occurred while generating the fixture "review_1" (App\Entity\Review):  
   Invalid value given for the property "publicationDate" of the object "review_  
  1" (class: App\Entity\Review).                                                  
                                                                                  

In HydrationExceptionFactory.php line 67:
                                                                                  
  Invalid value given for the property "publicationDate" of the object "review_1  
  " (class: App\Entity\Review).                                                   
                                                                                  

In PropertyAccessor.php line 522:
                                                                                  
  Cannot assign DateTime to property App\Entity\Review::$publicationDate of type  
   ?DateTimeImmutable 

This is because in Getting Started With API Platform: Create Your API and Your Jamstack Site you set the publicationDate to be DateTimeImmutable which should be DateTime

/** The publication date of this book. */
    public ?\DateTimeImmutable $publicationDate = null;

and

/** The date of publication of this review.*/
    public ?\DateTimeImmutable $publicationDate = null;

Should be

/** The publication date of this book. */
    public ?\DateTime $publicationDate = null;

and

/** The date of publication of this review.*/
    public ?\DateTime $publicationDate = null;

dalehurley avatar Jul 05 '22 22:07 dalehurley

Couldn't we fix the fixtures instead? Using immutable datetime objects is a best practice.

dunglas avatar Jul 06 '22 15:07 dunglas

Could you please suggest how the fixtures could be fixed?

dalehurley avatar Jul 06 '22 20:07 dalehurley