如何在“教义2”中设置日期? [英] How to set a date in Doctrine 2?

查看:113
本文介绍了如何在“教义2”中设置日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想使用doctrine创建一个添加到数据库的对象。



控制器内:

  $ name =John Alex; 
$ birthday =11-11-90;
$ student = new Student();
$ student-> setName($ name);
$ student-> setBirthday(strtotime($ birthday);
...

但是当我尝试坚持我得到这个错误

 致命错误:调用一个成员函数格式()第44行的/Library/WebServer/Documents/Symfony/vendor/doctrine-dbal/lib/Doctrine/DBAL/Types/DateType.php中的非对象

编辑:



我的实体:

  / ** 
* @var string $ name
*
* @ ORM\Column(name =name,type = string,length = 255)
* /
private $ name;

/ **
* @var date $ birthday
*
* @ ORM\Column(name =birthday,type =date,nullable = true)
* /
private $ birthday;

/ **
*设置生日
*
* @param date $ birthday
* /
public function setBirthday($ birthday)
{
$ this - > birthday = $ birthday;
}

/ **
*获取生日
*
* @return date
* $ b $基点公共函数getBirthday()
{
return $ this-> birthday;
}


解决方案



< pre class =lang-php prettyprint-override> $ name =John Alex;
$ birthday =11-11-1990; //我更改了
$ student = new Student();
$ student-> setName($ name);
$ student-> setBirthday(new \DateTime($ birthday)); //设置一个新的日期实例
// ...


I have a field named "birthday" in doctrine entity.

I would like to create an object to add to database using doctrine.

Inside the controller :

$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

Edit:

My entity:

/**
 * @var string $name
 *
 * @ORM\Column(name="name", type="string", length=255)
 */
private $name;

/**
 * @var date $birthday
 *
 * @ORM\Column(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
// ...

这篇关于如何在“教义2”中设置日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆