Doctrine 2: Call to a member function format() on a non-object … in DateTimeType.php

前端 未结 4 1759
北海茫月
北海茫月 2020-12-09 15:37

I have a DateTime field:

/**
 * Date time posted
 * @Column(type=\"datetime\")
 */
private $dtPosted;

which is set to a defau

相关标签:
4条回答
  • 2020-12-09 16:11

    you could always use:

    $this->updated = new \DateTime("now");
    

    http://www.doctrine-project.org/docs/orm/2.0/en/cookbook/working-with-datetime.html

    0 讨论(0)
  • 2020-12-09 16:13

    Try and use your setCreated with annotations for @ORM\PrePersist and setUpdated with annotations for @ORM\PrePersist and @ORM\PreUpdate methods as opposed to prePersist and preUpdate methods...

    /**
     * @ORM\PrePersist
     */
    public function setCreated()
    {
        $this->created = new \DateTime();
    }
    
    /**
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function setUpdated()
    {
        $this->updated = new \DateTime();
    }
    
    0 讨论(0)
  • 2020-12-09 16:26

    I came across a similar problem, but with a time field, and this question and @romanb 's answer helped.

    I was getting the following error, much like the one in the question.

    Call to a member function format() on a non-object in 
    ... /vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/TimeType.php on line 50
    

    The solution was similar, for fields of the time datatype, Doctrine will accept an instance of PHP's DateInterval

    $quizFixture1->setCompletionTime(\DateInterval::createFromDateString('743 seconds'));
    
    0 讨论(0)
  • 2020-12-09 16:36

    The date() function returns a string. The datetime type works with DateTime objects. So either change the mapping type to string or use DateTime objects.

    0 讨论(0)
提交回复
热议问题