I've create an Encryption service and want to access it inside an entity. Stn like this:
public function setCompanyName(string $companyName, Encryption $encryption)
{
$this->companyName = $encryption->encrypt($companyName);
}
But is it possible to do it without calling this function setCompanyName with two parameters? Do I have to inject container and call service inside the function?
You want to store encrypted data in db right ?
Best way to do this is to create event listener that 'll fire up on each entity save , and make encryption there (you can inject anything you want to listener) and second event listener that fire up when loading data from db to make decryption
If you do this right all encrytion/decryption thing will be transparent in code (it will only exists in those listeners )
look at this https://symfony.com/doc/3.4/doctrine/event_listeners_subscribers.html http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html