I have 2 entities: Country (id, name) and Mapping (id, object, internalId, externalId). Country and Mapping are not connected with associations (because Mapping has rows not
This might not be the best idea, but there is a simple way to do this.
The UnitOfWork class in doctrine will hydrate any entity which implements ObjectManagerAware with the entity manager and class metadata for that entity.
All you have to do to get the entity manger in your entity is implement the interface as shown in the example below:
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Persistence\ObjectManagerAware;
/**
* @ORM\Entity
*/
class MyEntity implements ObjectManagerAware
{
public function injectObjectManager(ObjectManager $objectManager, ClassMetadata $classMetadata)
{
$this->em = $objectManager;
}
}
If you create a new entity rather than querying it from the database, you will need to set the entity manager manually, for example with a setter method.