Doctrine orm 使用zf2中的条令继承映射标识实体的最干净方法

Doctrine orm 使用zf2中的条令继承映射标识实体的最干净方法,doctrine-orm,routing,zend-framework2,mapping,single-table-inheritance,Doctrine Orm,Routing,Zend Framework2,Mapping,Single Table Inheritance,我正在一个项目中使用条令继承映射,该项目生成一组唯一的实体,每个实体扩展一个基本实体。因为路由不知道哪些实体与哪些基本行一起,所以我必须查询数据库两次,以便从右侧字段集中获取我想要的行: // in a controller action: // locate the event entity record and determine the event type $entity = 'AdminEvents\Entity\Event'; $event = $this-

我正在一个项目中使用条令继承映射,该项目生成一组唯一的实体,每个实体扩展一个基本实体。因为路由不知道哪些实体与哪些基本行一起,所以我必须查询数据库两次,以便从右侧字段集中获取我想要的行:

// in a controller action:

    // locate the event entity record and determine the event type
    $entity = 'AdminEvents\Entity\Event';
    $event = $this->getEntityManager()->find($entity, $eventID);
    $eventType = $this->getEntityManager()->getClassMetadata(get_class($event))->discriminatorValue;

    // locate the record we're really looking for in the unique extended entity
    $entity = 'AdminEvents\Entity\\' . $eventType;
    $event = $this->getEntityManager()->find($entity, $eventID);

有更简洁的方法吗?

如果您还没有定义\AdminEvents\Entity\AbstractEvent类,那么您可能应该定义一个\AdminEvents\Entity\AbstractEvent类。然后,您的每个STI实体都应该对此进行扩展,您可以执行instanceof或其他逻辑来找出您得到的具体类型:

    // locate the record using the AbstractEntity
    $entity = 'AdminEvents\Entity\AbstractEntity';
    $event = $this->getEntityManager()->find($entity, $eventID);
注意:SPL函数get_类通常会返回Doctrine代理类,因此不要直接依赖它来测试返回类型。您可以使用原则类“ClassUtils”

\Doctrine\Common\Util\ClassUtils::getRealClass(get_class($event));