在 YML 夹具文件中生成数据(使用 PHP)

发布于 2024-08-21 16:42:27 字数 1120 浏览 2 评论 0原文

我想为夹具文件生成测试数据。我想要生成测试数据,而不必输入数百条记录。

假设我的架构如下所示:

  foobar_department_def:
    _attributes: { phpName: Department }
    id:
    name:             { type: varchar(64), required: true }

  foobar_qualification_def:
    _attributes: { phpName: Qualification }
    id:
    name:             { type: varchar(64), required: true }


  foobar_employee:
    _attributes: { phpName: Employee }
    id:
    first_name:       { type: varchar(64), required: true }
    last_name:        { type: varchar(64), required: true }
    biography:        { type: longvarchar, required: false }
    qualifi_id:       { type: integer, foreignTable: foobar_qualification_def, foreignReference: id, required: true, onUpdate: cascade, onDelete: restrict }
    dept_id:          { type: integer, foreignTable: foobar_department_def, foreignReference: id, required: true, onUpdate: cascade, onDelete: restrict }
    _uniques:
      idxu_fb_qly_dept:  [qualifi_id, dept_id]

How may I generated test data for jobs (using PHP in my YML file)?。我在 Symfony 文档中看到了这一点 - 然而,尽管再次搜索,我无法在 SF 网站上找到该页面(也许它已被删除?)

I want to generate test data for a fixture file. I wnat to generate the test data instead of having to type in hundreds of records.

Assuming my schema is as shown below:

  foobar_department_def:
    _attributes: { phpName: Department }
    id:
    name:             { type: varchar(64), required: true }

  foobar_qualification_def:
    _attributes: { phpName: Qualification }
    id:
    name:             { type: varchar(64), required: true }


  foobar_employee:
    _attributes: { phpName: Employee }
    id:
    first_name:       { type: varchar(64), required: true }
    last_name:        { type: varchar(64), required: true }
    biography:        { type: longvarchar, required: false }
    qualifi_id:       { type: integer, foreignTable: foobar_qualification_def, foreignReference: id, required: true, onUpdate: cascade, onDelete: restrict }
    dept_id:          { type: integer, foreignTable: foobar_department_def, foreignReference: id, required: true, onUpdate: cascade, onDelete: restrict }
    _uniques:
      idxu_fb_qly_dept:  [qualifi_id, dept_id]

How may I generate test data for employees (using PHP in my YML file)?. I saw this being done a little while agao, in the Symfony documentation - however, despite searching again, I can't locate the page on the SF website (maybe its been removed?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╭⌒浅淡时光〆 2024-08-28 16:42:27

我猜您正在寻找动态装置。并真正记住这一点:

  • 语句必须始终开始行或嵌入值中。
  • 如果语句结束一行,您需要明确输出一个新行< /strong>(“\n”)。


I guess you are looking for dynamic fixtures. And really keep this in mind:

  • The <?php ?> statements must always start the line or be embedded in a value.
  • If a <?php ?> statement ends a line, you need to explicly output a new line ("\n").
巡山小妖精 2024-08-28 16:42:27

Symfony 曾经(并且可能仍然)允许在其配置中使用 PHP YML 文件 但我不确定灯具文件。然而,听起来您尝试在 YML 文件中执行 PHP 是没有必要的,您只需要创建一个脚本来写入一个巨大的 YML 文件一次。

要生成固定装置,我建议在您定义的每个表中创建一行,然后将该数据转储到固定装置文件中,为您的数据提供模板。然后将该固定装置模板用于您的 PHP 脚本,用循环中生成的数据替换您的测试值。

Symfony used to (and probably still does) allow PHP in it's configuration YML files but I'm not sure about the fixtures files. However, for what it sounds like you're trying to do PHP in the YML file won't be necessary, you just need to create a script that writes a giant YML file once.

To generate your fixtures I would suggest creating one row in each of the tables you have defined and then dumping that data into a fixtures file to give a template for your data. Then use that fixtures template for your PHP script, replacing your test values with generated data in a loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文