Get array/list of entities from Doctrine

This is probably pretty simple, but I can't find a way to do this.

Is there any way to get a list of class names of the entities that Doctrine manages? Something like:

$entities = $doctrine->em->getEntities();

where $entities is an array with something like array('User', 'Address', 'PhoneNumber') etc...

Answers


I know this question is old, but in case someone still needs to do it (tested in Doctrine 2.4.0):

$classes = array();
$metas = $entityManager->getMetadataFactory()->getAllMetadata();
foreach ($metas as $meta) {
    $classes[] = $meta->getName();
}
var_dump($classes);

Source


Unfortunately not, your classes should be organized in the file structure though. Example: a project i'm working on now has all its doctrine classes in an init/classes folder.


There is no built function. But you can use marker/tagger interface to tag entity classes that belong to your application. You can then use the functions "get_declared_classes" and "is_subclass_of" find the list of entity classes.

For ex:

/**
 * 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;
}

Please note that my code sample above does not use namespaces for the sake simplicity. You will have to adjust it accordingly in your application.

That said you did't explain why you need to find the list of entity classes. Perhaps, there is a better solution for what your are trying to solve.


Need Your Help

Calling an external command in Python

python shell command subprocess external

How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?

Convert a binary tree to linked list, breadth first, constant storage/destructive

algorithm tree binary-tree

This is not homework, and I don't need to answer it, but now I have become obsessed :)