我有一个用户实体:
use Doctrine\ORM\Mapping as ORM;
/**
* ExampleBundle\Entity\User
*
* @ORM\Entity()
*/
class User
{
// ...
/**
* @ORM\Column(type="service_expires_at", type="date", nullable=true)
*/
private $service_expires_at;
public function getServiceExpiresAt()
{
return $this->service_expires_at;
}
public function setServiceExpiresAt(\DateTime $service_expires_at)
{
$this->service_expires_at = $service_expires_at;
}
}
Run Code Online (Sandbox Code Playgroud)
当我将用户更新service_expires_at为以下内容时,更新后的service_expires_at值不会保存回数据库:
$date = $user->getServiceExpiresAt();
var_dump($date->format('Y-m-d')); // 2013-03-08
$date->modify('+10 days');
var_dump($date->format('Y-m-d')); // 2013-03-18
$user->setServiceExpiresAt($date);
$em->persist($user);
$em->flush();
Run Code Online (Sandbox Code Playgroud)
但是,如果我传递一个新DateTime对象service_expires_at,则更新的值将正确保存:
$date = $user->getServiceExpiresAt();
$date->modify('+10 days');
$user->setServiceExpiresAt(new \DateTime($date->format('Y-m-d'));
$em->persist($user);
$em->flush();
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
我正在使用Doctrine来保存用户数据,我希望有一个last modification字段.以下是用户在按下后如何保存表单的伪代码Save:
last updated字段有问题的部分是if anything will be changed by this transaction.Doctrtrine可以给我这样的信息吗?
如何判断当前交易中的实体是否发生了变化?
编辑
只是为了清理,我试图修改一个被调用lastUpdated的实体中调用的字段,User如果提交User了当前事务,任何实体(包括但不限于)将被更改.换句话说,如果我启动事务并修改被调用nbCars实体的字段Garage,我希望更新实体的lastUpdated字段,User即使该实体尚未被修改.
我想在使用doctrine模式工具创建的数据库中保存日期时间.在我的表单中,我设置了日期和时间,我想将其保存为数据库中的日期时间.
所以我试过这个:
$e->setStartDateTime(new Zend_Date('2011-09-01T22:00:00',Zend_date::DATETIME));
但我得到错误:
PHP Fatal error: Call to undefined method Zend_Date::format() in /var/www/shared/Doctrine/lib/vendor/doctrine-dbal/lib/Doctrine/DBAL/Types/DateTimeType.php on line 44
Run Code Online (Sandbox Code Playgroud)
有没有人有这方面的经验,能够帮助我解决这个问题?