如何在 Doctrine 2 中设置日期? [英] How to set a date in Doctrine 2?
本文介绍了如何在 Doctrine 2 中设置日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在学说实体中有一个名为生日"的字段.
I have a field named "birthday" in doctrine entity.
我想使用学说创建一个要添加到数据库的对象.
I would like to create an object to add to database using doctrine.
控制器内部:
$name = "John Alex";
$birthday = "11-11-90";
$student = new Student();
$student->setName($name);
$student->setBirthday(strtotime($birthday);
...
但是当我尝试坚持时,我收到此错误
but when I try to persist I get this error
Fatal error: Call to a member function format() on a non-object in /Library/WebServer/Documents/Symfony/vendor/doctrine-dbal/lib/Doctrine/DBAL/Types/DateType.php on line 44
我的实体:
/**
* @var string $name
*
* @ORMColumn(name="name", type="string", length=255)
*/
private $name;
/**
* @var date $birthday
*
* @ORMColumn(name="birthday", type="date", nullable=true)
*/
private $birthday;
/**
* Set birthday
*
* @param date $birthday
*/
public function setBirthday($birthday)
{
$this->birthday = $birthday;
}
/**
* Get birthday
*
* @return date
*/
public function getBirthday()
{
return $this->birthday;
}
推荐答案
$name = "John Alex";
$birthday = "11-11-1990"; // I changed this
$student = new Student();
$student->setName($name);
$student->setBirthday(new DateTime($birthday)); // setting a new date instance
// ...
这篇关于如何在 Doctrine 2 中设置日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文