我正在尝试找到生成实体的最佳方法,这就是我目前正在做的事情。
我通过映射器和水合器创建了一个实体,如下所示:
namespace Event\Model\Mapper;
use ZfcBase\Mapper\AbstractDbMapper;
class Event extends AbstractDbMapper
{
protected $tableName = 'events';
public function findEventById($id)
{
$id = (int) $id;
$select = $this->getSelect($this->tableName)
->where(array('event_index' => $id));
$eventEntity = $this->select($select)->current();
if($eventEntity){
//Set Location Object
$locationMapper = $this->getServiceLocator()->get('location_mapper');
$locationEntity = $locationMapper->findlocationById($eventEntity->getLocationIndex());
$eventEntity->setLocationIndex($locationEntity);
//Set User Object
$userMapper = $this->getServiceLocator()->get('user_mapper');
$userEntity = $userMapper->findUserById($eventEntity->getEnteredBy());
$eventEntity->setEnteredBy($userEntity);
//Set Catalog Object
$catalogMapper = $this->getServiceLocator()->get('catalog_mapper');
$catalogEntity = $catalogMapper->findCatalogById($eventEntity->getCatalogIndex());
$eventEntity->setCatalogIndex($catalogEntity);
}
return $eventEntity;
}
}
现在这种方法的问题是,当我打电话说用户实体时,这个实体有其他实体附加到它上面,所以当我通过插入用户实体生成事件实体时,我的事件实体变得非常大而且笨重,我不希望我只想要“老年学树”的第一层。
所以我正在考虑创建一个 EventEntityFactory ,我可以将 Event 实体的子实体绑定在一起,我正计划为此做一个工厂。
有没有更好的方法来做到这一点?
谢谢