I was working on a project that dealt with sport analytics and Symfony2 forms were very slow because I had too much many-to-many and one-to-many entities loaded. Those kinds of problems are easily fixed using data transformers. You can read more about them here.
The reason why I’m writing this blog post is because I’ve set it up a little bit differently. All those entities that were causing the problem required the same transformer. The only thing that differed was the repository. Because of those reasons there was no need to needlessly replicate data transformers and custom field types.
Thus I’ve used dependency injection. In data transformer in addition to injecting object manager I’ve injected the repository and in the custom field type I injected name of custom field type.
Below you can see an example of that implementation.
This is data transform which was injected with repository and object manager.
Here you can see a custom filed type. In addition to object manager and repository I had to inject name of custom field type.
This is my services.xml file. I prefer using xml for definining my services because when I’ve used yml format too often I had problem with wrong indentation.
Finally after all that configuration you can use your custom field types.
Now you just have to render your forms.
This post is on GitHub. If you spot any errors please do a pull request.
comments powered by Disqus