Generating data (using PHP) in a YML fixtures file
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, 开发者_如何学PythonforeignReference: 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?)
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").
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.
精彩评论