相关疑难解决方法(0)

Doctrine2 ORM不会保存对DateTime字段的更改

我有一个用户实体:

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)

为什么会这样?

oop encapsulation doctrine-orm

21
推荐指数
1
解决办法
6673
查看次数

如何判断当前事务是否会更改具有Doctrine 2的任何实体?

我正在使用Doctrine来保存用户数据,我希望有一个last modification字段.以下是用户在按下后如何保存表单的伪代码Save:

  • 开始交易
  • 做很多事情,可能是查询数据库,可能不是
  • 如果此交易有任何改变
    • 修改last updated字段
  • 提交事务

有问题的部分是if anything will be changed by this transaction.Doctrtrine可以给我这样的信息吗?

如何判断当前交易中的实体是否发生了变化?

编辑

只是为了清理,我试图修改一个被调用lastUpdated的实体中调用的字段,User如果提交User了当前事务,任何实体(包括但不限于)将被更改.换句话说,如果我启动事务并修改被调用nbCars实体的字段Garage,我希望更新实体的lastUpdated字段,User即使该实体尚未被修改.

php mysql doctrine-orm

3
推荐指数
2
解决办法
2518
查看次数

使用Doctrine 2.1将Zend日期保存在数据库中

我想在使用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)

有没有人有这方面的经验,能够帮助我解决这个问题?

php zend-framework zend-date doctrine-orm

2
推荐指数
1
解决办法
2925
查看次数