34

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 only for country). I need to get external id for country using following conditions:

  • country.id = mapping.internalId
  • mapping.object = 'country'

So I plan to add function getExternalId() in Country

function getExternalId() {
    $em = Registry::getEntityManager();

    $mapping = $em->getRepository('Mapping')->findOneBy(array(
        'object'     => 'country',
        'internalId' => $this->getId()
    ));

    return !empty($mapping) ? $mapping->getExternalId() : false;
}

Questions:

  1. Is it good practice to use EntityManager inside entities? If no, please explain how to get external id in my case?
  2. Maybe it is possible to associate Country and Mapping using yaml files?

Thanks in advance!

4 Answers 4

52

It is not a good idea to allow an entity object to rely on the entity manager. It ties the entity to the persistence layer, which was a problem Doctrine 2 was specifically trying to solve. The biggest hassle in relying on the entity manager is that it makes your model hard to test in isolation, away from the database.

You should probably be relying on service objects to handle the operations that rely on the entity manager.

// CountryService
public function getExternalId($country) {}

Additionally, you could create proxy methods on your model to call out to a service object that is set externally. A service object would be much easier to mock while testing than the entity manager would be.

$country->setService($countryService);
$country->getExternalId();

// Country
public function getExternalId()
{
   $this->_service->getExternalId($this);
}  
3
  • 6
    I am looking for good tutorials about service implementation for doctrine. Does somebody know good tutorials.
    – user257980
    Apr 6, 2011 at 11:19
  • Does the Service Object in this instance need to be internal to the entity or can you use it outwith the entity? for example. $service = new Country\ServiceObject; $ExtId = $service->getExternalId($country); Jun 24, 2011 at 8:40
  • service implementation for doctrine : symfony.com/doc/current/book/service_container.html Mar 12, 2013 at 19:16
13

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.

0
10

I think what you need to be using are Entity Repositories. These are detailed in the documentation, albeit a bit hard to find information on. Here is a link to the Getting Started article which documents how one would create a "repository" of 'access' functions for your entities.

Additionally here is some pseudo-code to get you started:

<?php
// repositories/CountryRepository.php

use Doctrine\ORM\EntityRepository;

class CountryRepository extends EntityRepository
{
    public function getExternalId()
    {
2
  • looks like the link is dead
    – Gigala
    Jun 11, 2013 at 7:09
  • 1
    At this moment, the link is working. I think this is a very good solution to the OP's question. Nov 15, 2013 at 15:53
1

A slightly cutting edge addendum to this (PHP 5.4 being at alpha 2 at time of this post) which may be of use in the future:

Here are some examples of using php 5.4 traits within Doctrine2; one of which is called active entity and provides active record style functionality within Doctrine 2 including access to the entity manager from within the entity.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.