2

This is my controller class

/**
 * @Route("/category/management/new", name="category_new", methods={"GET","POST"})
 * @param Request $request
 * @return Response
 * @throws Exception
 */
public function createCategory(Request $request): Response
{

    $date= new DateTime();
    $form =$this->createFormBuilder()
        ->add('name',TextType::class)
        ->add('tags',TextType::class,[
            'required'=>false,
            ])
        ->add('parent', EntityType::class, [
            'label'=> 'If this is a subcategory please select a parent',
            'required'=>false,
            'class' => Category::class,
            'choice_label' => 'name',
        ])
        ->add('Create',SubmitType::class)
    ->getForm();
    $form->handleRequest($request);

    $user=$this->get('security.token_storage')->getToken()->getUser();

    var_dump($date);

    if($form->isSubmitted()){
        $data = $form->getData();
        $category = new Category();
        $category->setName($data['name']);
        $category->setTags($data['tags']);
        $category->setParent($data['parent']);
        $category->setCreatedAt(new DateTime());
        $category->setCreatedBy($user);

        $em = $this->getDoctrine()->getManager();
        $em->persist($category);
        $em->flush();

        return $this->redirect($this->generateUrl('category_management'));
    }

    return $this->render('category_management/create.html.twig',[
        'form' => $form->createView()
    ]);

i've tried running it without setCreatedAt(new Datetime()) and relying on the DB to do the work but it still errors out.

Tried making changes in the entity class and directly setting New Datetime in the setCreatedAt method

All these didnt work and im stuck

  • $createdAt = 'CURRENT_TIMESTAM', like this you pass a string to datetime attribute. what about pass it in the constructor `function __construct() { $this->createdAt = new Datetime(); } ` – hous Nov 16 '20 at 15:29
  • i tried passing it in the constructor but it still errored out – Simeon Simeonovv Nov 16 '20 at 15:43
  • before your last edit, I saw $createdtAt = 'CURRENT_TIMESTAM', so remove = 'CURRENT_TIMESTAM', – hous Nov 16 '20 at 15:46
  • Still the same error. The ironic thing is that my CRUD for my user class uses the same methods and annotations and works fine – Simeon Simeonovv Nov 16 '20 at 15:48
  • you said "still the same error" , but if you delete "CURRENT_TIMESTAM" , the error should be changed and no more display "CURRENT_TIMESTAM" in the error message. Try to clear the cache if you use prod env – hous Nov 16 '20 at 15:56
  • See this answer might help https://stackoverflow.com/a/50467860/12232340 –  Nov 16 '20 at 16:10
  • https://stackoverflow.com/questions/59427778/symfony-4-must-implement-interface-datetimeinterface-error this solved my problem – Simeon Simeonovv Nov 17 '20 at 12:53
  • Maybe this lib is helpfull for you : https://github.com/Atlantic18/DoctrineExtensions/tree/v2.4.x/lib/Gedmo Replace createdAt in your entity with: use TimestampableEntity; – Tim Zwinkels Nov 20 '20 at 19:58

0 Answers0