oracle-databasesymfonysymfony5sysdate

Symfony entity - Oracle default SYSDATE not working


Symfony 5 + Oracle.

Entity:

/**
 * @var \DateTime
 *
 * @ORM\Column(name="CREATE_DT", type="date", nullable=false, options={"default"="SYSDATE"})
 */
private $createDt = 'SYSDATE';

Controller:

//save entity object to database (createDt property NOT passed, default must be applied)
$em->persist($obj);
$em->flush();

Error: Could not convert PHP value 'SYSDATE' of type 'string' to type 'date'. Expected one of the following types: null, DateTime (500 Internal Server Error)

How can I make Symfony apply default SYSDATE at flush?


Solution

  • You can init your date with default value directly in the constructor.

    class YourEntity {
    
        /**
         * @var \DateTime
         *
         * @ORM\Column(name="CREATE_DT", type="date", nullable=false)
         */
        private $createDt;
    
        public function __construct() {
            $this->createDt = new \Datetime();
        }
    }