[英]How is it possible to get doctrine entities from a query builder in a 2D array?
[英]Get array/list of entities from Doctrine
這可能很簡單,但我找不到辦法做到這一點。
有沒有辦法獲得 Doctrine 管理的實體的類名列表? 就像是:
$entities = $doctrine->em->getEntities();
其中$entities
是一個數組,類似於array('User', 'Address', 'PhoneNumber')
等...
我知道這個問題很老,但以防萬一有人仍然需要這樣做(在 Doctrine 2.4.0 中測試):
$classes = array();
$metas = $entityManager->getMetadataFactory()->getAllMetadata();
foreach ($metas as $meta) {
$classes[] = $meta->getName();
}
var_dump($classes);
獲取所有實體的類名(帶命名空間)的另一種方法是:
$entitiesClassNames = $entityManager->getConfiguration()->getMetadataDriverImpl()->getAllClassNames();
不幸的是,您的類應該在文件結構中組織。 示例:我現在正在處理的一個項目在 init/classes 文件夾中包含所有的學說類。
沒有內置功能。 但是您可以使用標記/標記器接口來標記屬於您的應用程序的實體類。 然后您可以使用函數“get_declared_classes”和“is_subclass_of”查找實體類列表。
例如:
/**
* Provides a marker interface to identify entity classes related to the application
*/
interface MyApplicationEntity {}
/**
* @Entity
*/
class User implements MyApplicationEntity {
// Your entity class definition goes here.
}
/**
* Finds the list of entity classes. Please note that only entity classes
* that are currently loaded will be detected by this method.
* For ex: require_once('User.php'); or use User; must have been called somewhere
* within the current execution.
* @return array of entity classes.
*/
function getApplicationEntities() {
$classes = array();
foreach(get_declared_classes() as $class) {
if (is_subclass_of($class, "MyApplicationEntity")) {
$classes[] = $class;
}
}
return $classes;
}
請注意,為了簡單起見,我上面的代碼示例沒有使用命名空間。 您必須在您的應用程序中相應地調整它。
也就是說,您沒有解釋為什么需要查找實體類列表。 也許,對於您要解決的問題,有更好的解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.