Shawn
Shawn

Reputation: 11391

Why doesn't Doctrine 2 find my entities?

I'm trying to learn about using Doctrine 2 by making a very simple script which fetches information from my database. The problem is that I can't find any documentation explaining how Doctrine finds and uses my mapping entities. And so when it complains that it's not finding an entity, I don't know what to do to solve the problem. Consider the following structure in my www folder:

person.php

<?php

/** @Entity @Table(name="person")*/
class person
{
    /**
     * @Id @Column(type="integer")
     */
    protected $uid;
}
?>

myTestPage.php

<?php
    require "Doctrine/Doctrine/ORM/Tools/Setup.php";

    $lib = "Doctrine";
    Doctrine\ORM\Tools\Setup::registerAutoloadDirectory($lib);

    use Doctrine\ORM\Tools\Setup,
        Doctrine\ORM\EntityManager;

    $paths = array("/Entities");

    $isDevMode = true;

    $dbParams = array("driver" => "pdo_mysql",
        "host" => "myhost.ca",
        "user" => "Shawn",
        "password" => "noneofyourbusiness",
        "dbname" => "testDB");

    $config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
    $em = EntityManager::create($dbParams, $config);

    $qb = $em->createQueryBuilder();
    $qb->select(array('uid'))
        ->from('person', 't');
    $query = $qb->getQuery();

    $result = $query->getResult();
    echo $result;
?>

When I visit myTestPage.php, I get the following error message:

Fatal error: Uncaught exception 'Doctrine\ORM\Query\QueryException' with message '[Semantical Error] line 0, col 24 near 'person': Error: Class 'person' is not defined.' in C:\wamp\www\Doctrine\Doctrine\ORM\Query\QueryException.php on line 47

The problem seems to be that Doctrine can't find person.php, but how can I solve this?

Upvotes: 0

Views: 5875

Answers (1)

Ivan Pintar
Ivan Pintar

Reputation: 1881

I don't exactly know what the "re1_chercheur" means, but when I tried your code, it complained about person not being defined, so i included that and it worked. Of course, this should be dealt with by using an autoloader of some sort, but I think doctrine has some solution for that also. I haven't been using their autoloader, because I have my own which uses the PSR compliant autoloading.

Also in your select, if you want just the "uid", use "t.uid". "t" is the alias of the person class, so in other places in the query, you'll be using "t" to get to it's properties (you could do a ->where("t.uid = someNumber")). If you want the whole object use select("t");

I found the doctrine documentation quite helpful, just make sure you go in the order it is laid out, and you'll get the hang of it in no time.

I know this is not an exact answer, but maybe it points you into the right direction.

Upvotes: 2

Related Questions