开发者

Symfony2 entity dependencies/app parameters

开发者 https://www.devze.com 2023-04-11 06:10 出处:网络
I\'m pretty new to Symfony2 and I\'m trying to get to grips with how and when to pass dependencies / app parameters and have got into a muddle with how to insert parameters into an entity.

I'm pretty new to Symfony2 and I'm trying to get to grips with how and when to pass dependencies / app parameters and have got into a muddle with how to insert parameters into an entity.

The开发者_运维百科 situation is that I have an entity which will contain strings of uploaded file names and I want to pass through the parameters of the directory location (where the uploaded file will be stored) which I have set in app/config.yml. Which is basically similar to http://symfony.com/doc/2.0/cookbook/doctrine/file_uploads.html but with the paths defined in app/config.yml rather than hard coded into the entity.

First off I thought this could be done via the constructor, but this only seems fine for new objects and not when they are pulled out the repository? (as the constructor is not called then) so I don't know how you're supposed to pass the dependencies to the entites.

Any guidance much appreciated.


I think this problem should be solved by adding a listener for onLoad (and maybe onPersist, not sure) events.

In the entity add a setUploadPath($path) method, which you will call with the listener.

In order to actually have the path parameter in the listener you can pass it as constructor argument when setting up the listener service.

0

精彩评论

暂无评论...
验证码 换一张
取 消