alice icon indicating copy to clipboard operation
alice copied to clipboard

Using references in escaped string

Open vuryss opened this issue 3 years ago • 4 comments

I have the following case:

App\Entity\News:
  news1:
    id: '<uuid()>'
App\Entity\Preference:
  recommendedNews:
    value: '\["@news1->id"]'

Unfortunately once the array is escaped - meaning I want it as string - the contents of the [ ] are not lexed at all. I tried to find some weird way to get into one of the other regexes, but I couldn't.

My idea is to be able to construct JSON array (in this case) containing references to other entities.

Is that possible in some way or planned to be implemented in the future?

vuryss avatar Oct 14 '21 07:10 vuryss

Unfortunately I have to plan to work on the lexer bugs in a near future

theofidry avatar Oct 15 '21 06:10 theofidry

Can you use relationships in your database instead?

Or did you try to use an array in the fixtures? Something like that:

App\Entity\Preference:
  recommendedNews:
    value: ["@news1->id"]

Then the setValue() could detect when an array is provided and transform it to a JSON array.

alexislefebvre avatar Oct 15 '21 11:10 alexislefebvre

Can you use relationships in your database instead?

Or did you try to use an array in the fixtures? Something like that:

App\Entity\Preference:
  recommendedNews:
    value: ["@news1->id"]

Then the setValue() could detect when an array is provided and transform it to a JSON array.

I don't like to design entities around DB fixtures, it shouldn't accept arrays and convert them to JSON just so that test data gets generated correctly. IMHO this is a bad practice. Unless you actually need that behavior in your normal application.

Moreover this string field might not be necessary in JSON format for all records. In my case, this is a general configuration table - records will be different according to their name.

For now I hardcoded some UUIDs so I don't have to explicitly reference them, but it would be nice if you can use references inside a string.

vuryss avatar Oct 15 '21 12:10 vuryss

I agree that it's bad design, my suggestion was a workaround to avoid being stuck with the error when loading fixtures.

alexislefebvre avatar Oct 15 '21 13:10 alexislefebvre