Symfony fixtures - 是否可以 'dummy' 创建日期?
Symfony fixtures - is it possible to 'dummy' create dates?
我尝试创建依赖于其他实体 createDT 标记的固定装置。但是,我无法 'spoof' 这些,因为我使用 @prepersist
来填充实体中的 createDT 字段。我有很多涉及的实体,我想知道是否有任何方法可以暂时 freeze/override 这个 @prepersist
只是为了加载我的固定装置?
我的实体都有:
/**
* Set createDT
* @ORM\PrePersist
*/
public function setCreateDT()
{
$this->createDT = new \DateTime();
return $this;
}
...我正在尝试以下面的方式有条件地指定固定装置的值,与另一个实体的 createDT
进行比较:
class LoadTransactionData extends AbstractFixture implements OrderedFixtureInterface {
public function load(ObjectManager $manager) {
for ($month=1; $month <= 120; $month++) {
$transaction = new Transaction();
$date = new \DateTime();
if($date->setDate(2000 + floor($month/12),$month % 12,1) < $this->getReference('AC_1')->getCreateDT()) {
// ...get references etc and do stuff here....
} else {
// ...get references etc and do other stuff here...
}
$manager->persist($transaction);
}
$manager->flush();
}
更新: 以上是最好的,但如果不可能,如果有办法延迟某些记录的持久化,也许就足够了?这样,我至少可以使时间戳相差一两秒进行比较
一个解决方案可以在构造函数方法中分配日期:
public function __construct()
{
$this->createDT = new \DateTime();
}
因此,当您的灯具加载时,此日期将被覆盖。
我尝试创建依赖于其他实体 createDT 标记的固定装置。但是,我无法 'spoof' 这些,因为我使用 @prepersist
来填充实体中的 createDT 字段。我有很多涉及的实体,我想知道是否有任何方法可以暂时 freeze/override 这个 @prepersist
只是为了加载我的固定装置?
我的实体都有:
/**
* Set createDT
* @ORM\PrePersist
*/
public function setCreateDT()
{
$this->createDT = new \DateTime();
return $this;
}
...我正在尝试以下面的方式有条件地指定固定装置的值,与另一个实体的 createDT
进行比较:
class LoadTransactionData extends AbstractFixture implements OrderedFixtureInterface {
public function load(ObjectManager $manager) {
for ($month=1; $month <= 120; $month++) {
$transaction = new Transaction();
$date = new \DateTime();
if($date->setDate(2000 + floor($month/12),$month % 12,1) < $this->getReference('AC_1')->getCreateDT()) {
// ...get references etc and do stuff here....
} else {
// ...get references etc and do other stuff here...
}
$manager->persist($transaction);
}
$manager->flush();
}
更新: 以上是最好的,但如果不可能,如果有办法延迟某些记录的持久化,也许就足够了?这样,我至少可以使时间戳相差一两秒进行比较
一个解决方案可以在构造函数方法中分配日期:
public function __construct()
{
$this->createDT = new \DateTime();
}
因此,当您的灯具加载时,此日期将被覆盖。