I'm setting a YAML file for an entity, and there are some related fields that are already populated in database, so I want to reference those values in the current YAML file.
For example, status
field in next sample should load an App\Entity\Status
that already exists in database (1 to 3 are existing IDs, but it fails):
App\Entity\Dummy:
dummy_{1..10}:
name: '<firstName()>'
surnames: '<lastName()>'
email (unique): '<email()>'
startDate: '<dateTimeBetween("-200 days", "now")>'
subject: '<sentence()>'
status: '<numberBetween(1, 3)>'
When loading this file with --append
option, I get this error:
Invalid value given for the property "status" of the object "dummy_1" (class: App\Entity\Dummy)
Expected argument of type "?App\Entity\Status", "int" given at property path "status".
How can I reference a pre-existing entity?
About the "related" question, in that question the related entity is loaded in a separate YAML file (not my case, the related entity is already in database), and that question does not have an answer.
Straight from the docs:
Nelmio\Entity\Group:
group1:
owner: '@user<numberBetween(1, 200)>'
In your case, it would be:
App\Entity\Status:
status_1
name: "Foo"
status_2
name: "Bar"
status_3
name: "Baz"
App\Entity\Dummy:
dummy_{1..10}:
name: '<firstName()>'
surnames: '<lastName()>'
email (unique): '<email()>'
startDate: '<dateTimeBetween("-200 days", "now")>'
subject: '<sentence()>'
status: '@status_<numberBetween(1, 3)>'
For pre-existing data, not coming from a fixture... you'll have more difficulties. I'd personally set up everything with the fixtures, which is their purpose. Saving on a few DB transactions is not worth the effort and the uncertainty.
Check this issue for more info and approaches.