I know this question seems horrible at first glance.
This is what I have:
I have part of my model stored in *.yaml file and loaded when it's needed. Let's call it StaticObject. It have 10 or so objects, is not scaleable by design and read-only. Each StaticObject has own ID and I can get object with given ID from service in container, like:
$container->get('static_object_repository')->getById('foo');
Now I have some entity which has "association" with that StaticObject. I want it to be stored in database as ID, so I added new field $staticObjectId to entity, like this:
class MyEntity
{
//...
/**
* @ORM\Column(type="string", name="static_object_id")
*/
protected $staticObjectId;
//...
}
Now every time I want to get StaticObject from entity, I call service from my container, like:
$staticObject = $container->get('static_object_repository')->getById($entity->getStaticObjectId());
and when I want to change that associated object, I do something like:
$entity->setStaticObjectId($staticObject->getId());
I hope you got enough information about my code. Tell me if I miss something important for you to understand problem.
This is what I want:
I want to store StaticObject instance directly in entity, so I can simply get and set it. Just simple
$entity->getStaticObject();
//and
$entity->setStaticObject($staticObject);
My first thought was to hide all needed transformation in getter and setter. So I get:
class MyEntity
{
//...
public function setStaticObject(StaticObject $so)
{
$this->staticObjectId = $so->getId();
}
public function getStaticObject()
{
return $container->get('static_object_repository')->getById($this->staticObjectId);
}
//...
}
But wait... I cannot have container in my model. Doesn't I? I can find nowhere how to inject container/service to entity.
My second thought was to use custom Doctrine mapping type. And again - you cannot inject service to it (since custom type is class and no object/service). I did everything from this section of Doctine2 documentation and i read this section in Symfony2 cookbook, but I cannot see solution.
How can I do this?