【问题标题】:Create sane start and end dates with nelmio\alice使用 nelmio\alice 创建合理的开始和结束日期
【发布时间】:2017-05-07 14:03:31
【问题描述】:

我想创建一个遵循 orderDatetime 字段中的日期时间的 completedDatetime。

Fixtures.yml

directive_{200..500}:
    orderDatetime: <dateTimeThisYear()>
    completedDatetime: '90%? <dateTimeBetween("orderDatetime", "now")>'

我在我的fixtures 文件中使用了上面的代码并得到了下面的数据。

除了在 LoadFixtures 中编写自定义函数之外,有没有办法使用伪造的数据来确保一个健全的结果??

【问题讨论】:

    标签: doctrine symfony faker nelmio-alice alice-fixtures


    【解决方案1】:

    由于您将字符串“orderDatetime”传递给strtotime() 函数,它返回1970 年并且您的dateTimeBetween 的工作方式类似于dateTimeBetween('1970', 'now')。变量的传递是使用$orderDatetime 完成的。但是如果你将这样的变量传递给dateTimeBetween(),未来的日期时间可能会被传递,这是不可能的。

    仅当您的订单日期已过时才有效:

    orderDatetime: '<dateTimeBetween("-200 days", "now")>'
    completedDatetime: '90%? <dateTimeBetween($orderDatetime, "now")>'
    

    如果您不想生成未来的日期,那么您需要创建自定义的 faker 函数,例如:

    public function dateTimeBetweenAfter($dateFrom, $dateTo)
    {
        $date = new DateTime($dateFrom->format('Y-m-d H:i:s'));
        $dateTo = $date->modify($dateTo);
    
        return FakerDateTime::dateTimeBetween($dateFrom, $dateTo);
    }
    

    并像这样使用它:

    orderDatetime: <dateTimeThisYear()>
    completedDatetime: '90%? <dateTimeBetweenAfter($orderDatetime, "+1 year")>'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-20
      相关资源
      最近更新 更多