php - Inject a parameter into an entity in symfony2 -


i feel silly asking question can't find clear answer anywhere.

i have token entity has, among other things, creation_time.

i have parameter in parameters.yml called token_lifespan.

for token, if creation_time < (time() - token_lifespan) has expired.

the problem can't find nice way pass token_lifespan parameter entity. know like:

$token->hasexpired($this->getparameter('token_lifespan')) 

but feels icky. i've been looking @ making service, , dependency injection, way pass token lifespan token when created, can't work out i'm doing.

i'm making tokens $token = new mytoken(); , i'm getting tokens doctrine so:

$this->getdoctrine()     ->getrepository('mybundle:mytoken')     ->find($token_id); 

do need make repository service? like:

mytoken_repository:     class:  mybundle\entity\mytoken     factory_service: 'doctrine.orm.default_entity_manager'     factory_method:  'getrepository'     calls:          - [setlifespan, ['%token_lifespan%']] 

and mean? still create tokens same way, new mytoken()? , make setlifespan method in mytoken class store lifespan?

finally, can still tokens doctrine $this->getdoctrine()...? , if need use token_lifespan parameter in repository class?

sorry there's million questions in there. feel dependency injection part of symfony docs assumes know lot more framework do.

you should logic out of entity , make service it. entity should not have dependencies.

you configure service have %token_lifespan% argument , make method this:

/**  * @return boolean  */ function isexpired(token $token) {    //your logic here, add dependencies constructor , "arguments:" in service configuration } 

i hope gets started.


Comments