【问题标题】:Symfony fixtures - is it possible to 'dummy' create dates?Symfony 固定装置 - 是否可以“虚拟”创建日期?
【发布时间】:2016-09-23 14:38:30
【问题描述】:

我正在尝试创建依赖于其他实体 createDT 标记的固定装置。但是,当我使用 @prepersist 填充我的实体中的 createDT 字段时,我无法“欺骗”这些内容。我有很多实体参与其中,我想知道是否有任何方法可以暂时冻结/覆盖这个 @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();
    }

更新:以上是最好的,但如果不可能,如果有办法延迟某些记录的持久化可能就足够了?这样,我至少可以做出一两秒的时间戳差异来进行比较

【问题讨论】:

    标签: doctrine-orm symfony fixtures


    【解决方案1】:

    一种解决方案可以在 constructor 方法中分配日期:

    public function __construct()
    {
        $this->createDT = new \DateTime();
    }
    

    因此,当您的装置被加载时,此日期将被覆盖。

    【讨论】:

    • 谢谢 - 我很有希望,因为这似乎是个好主意,但不幸的是没有运气! PrePersist 仍在捕获它并使用当前 DT 标记填充记录
    • 这行得通,谢谢 :-) 不得不断开所有 PrePersists 的连接很可惜,但至少它行得通!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多