DoctrineBehaviors: Using Timestampable throws an error when creating a new entity

I changed the entity like described in @TomasVotruba blog https://www.tomasvotruba.com/blog/2020/01/06/migrate-gedmo-to-knplabs/ thinking it was very easy.

I can change an existing record and it captures the updatedAt time. But when creating a new record, an error is shown.

Can anyone help me with this?

TypeError:
Return value of App\Entity\Product::getCreatedAt() must implement interface DateTimeInterface, null returned

  at vendor/knplabs/doctrine-behaviors/src/Model/Timestampable/TimestampableMethodsTrait.php:16
  at App\Entity\Product->getCreatedAt()
     (vendor/symfony/property-access/PropertyAccessor.php:384)
  at Symfony\Component\PropertyAccess\PropertyAccessor->readProperty()
     (vendor/symfony/property-access/PropertyAccessor.php:93)
  at Symfony\Component\PropertyAccess\PropertyAccessor->getValue()
     (vendor/symfony/form/Extension/Core/DataMapper/PropertyPathMapper.php:49)
  at Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper->mapDataToForms()
     (vendor/symfony/form/Form.php:377)
  at Symfony\Component\Form\Form->setData()
     (vendor/symfony/form/Form.php:481)
  at Symfony\Component\Form\Form->initialize()
     (vendor/symfony/form/FormBuilder.php:206)
  at Symfony\Component\Form\FormBuilder->getForm()
     (vendor/easycorp/easyadmin-bundle/src/Controller/AdminControllerTrait.php:842)
  at EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController->createEntityForm()
     (vendor/easycorp/easyadmin-bundle/src/Controller/AdminControllerTrait.php:778)
  at EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController->createNewForm()
     (vendor/easycorp/easyadmin-bundle/src/Controller/AdminControllerTrait.php:908)
  at EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController->executeDynamicMethod()
     (vendor/easycorp/easyadmin-bundle/src/Controller/AdminControllerTrait.php:301)
  at EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController->newAction()
     (vendor/easycorp/easyadmin-bundle/src/Controller/AdminControllerTrait.php:908)
  at EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController->executeDynamicMethod()
     (vendor/easycorp/easyadmin-bundle/src/Controller/AdminControllerTrait.php:86)
  at EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController->indexAction()
     (vendor/symfony/http-kernel/HttpKernel.php:157)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw()
     (vendor/symfony/http-kernel/HttpKernel.php:79)
  at Symfony\Component\HttpKernel\HttpKernel->handle()
     (vendor/symfony/http-kernel/Kernel.php:191)
  at Symfony\Component\HttpKernel\Kernel->handle()
     (public/index.php:25)
  at require('/home/paul/www/test-knplabs/public/index.php')
     (/home/paul/.symfony/php/1f578ba6ccbb1add18b380021c9ebe0f3fd87e3c-router.php:29)   

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 20 (18 by maintainers)

Most upvoted comments

This is a simple a if we create a new Entity and call getCreated()/getUpdated() it should not failed.

The method signature in the trait should simply allow to return null value.

Edit: The interface too.

Hi @TomasVotruba, I think you can close this issue 😃