我想做从实体获取表名的相反操作:
我想从表名中获取实体名称,即映射到该表的实体的名称。
db 列也是如此:如何获取映射到它的实体的字段名称?
这是我能够做的,虽然它不是最佳的,因为它会遍历所有注册的实体类名称:
/**
* @param \Doctrine\ORM\EntityManager $em Entity manager
* @param string $table Table name
* @return string Entity class name, null if not found
*/
protected function getClassNameFromTableName($em, $table)
{
// Go through all the classes
$classNames = $em->getConfiguration()->getMetadataDriverImpl()->getAllClassNames();
foreach ($classNames as $className) {
$classMetaData = $em->getClassMetadata($className);
if ($table == $classMetaData->getTableName()) {
return $classMetaData->getName();
}
}
return null;
}
/**
* @param \Doctrine\ORM\EntityManager $em Entity manager
* @param string $className
* @param string $column
* @return string Field name, null if not found
*/
protected function getFieldNameFromColumnName($em, $className, $column)
{
$classMetaData = $em->getClassMetadata($className);
if ($classMetaData) {
return $classMetaData->getFieldForColumn($column);
}
return null;
}