在Doctrine 2.0实体中使用EntityManager [英] Using EntityManager inside Doctrine 2.0 entities

查看:139
本文介绍了在Doctrine 2.0实体中使用EntityManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个实体:Country(id,name)和Mapping(id,object,internalId,externalId)。
国家和地图与关联不相关(因为映射行不仅适用于国家/地区)。我需要使用以下条件获取国家的外部ID:




  • country.id = mapping.internalId

  • mapping.object ='country'



所以我打算在国家/地区添加函数getExternalId()

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

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

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

问题:


  1. 在实体中使用EntityManager是个好习惯吗?如果没有,请说明如何在我的情况下获取外部ID?

  2. 也许可以使用yaml文件关联国家/地区和映射?

提前感谢

解决方案

允许一个实体不是一个好主意反对依靠实体经理。它将实体与持久层联系起来,这是Doctrine 2专门解决的一个问题。依靠实体经理最大的麻烦在于它使您的模型难以孤立地测试,远离数据库。



您应该可以依靠服务对象处理依赖于实体管理器的操作。

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

此外,您可以在模型上创建代理方法,以调用外部设置的服务对象。一个服务对象在测试时要比实体管理员容易一些。

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

//国家
public function getExternalId()
{
$ this-> _service-> getExternalId($ this);
}


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!

解决方案

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);
}  

这篇关于在Doctrine 2.0实体中使用EntityManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆