Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Doctrine Beginner: Doctrine\ORM\Tools\Setup not found

im a beginner with doctrine. I just installed pear + doctrine 2.3.3 and want to test it.

to test doctrine i wrote a class named "person"

/**
 * @Entity
 */
class person
{
    /** @Id @Column(type="integer") @GeneratedValue * */
    private $id;

    /** @Column(type="string") * */
    private $name;

    /** @Column(type="string") * */
    private $surname;

    //some getters and setters ...
}

after that i made my bootstrap.php file, bootstrep_doctrine.php and cli-config.php files and run the command:

doctrine orm:schema-tool:create

that works fine!

But now, when i want to include my bootstrap.php in a "normal" php file, to create a "person" i get the following error:

Fatal error: Class 'Doctrine\ORM\Tools\Setup' not found 
in /var/www/vms/bootstrap_doctrine.php on line 15

The file looks like follows:

<?php
$debug = true;
if($debug){
    error_reporting(E_ALL);
    ini_set("display_errors", "on");
    ini_set("display_startip_errors", "on");
}
require_once '../bootstrap.php';

include_once ('testClassOrm.php');
$person = new person();
$person = new person();
$person->setName("Hans");
?>

bootstrap.php:

if(!class_exists("Doctrine\Common\Version", false)){
    require_once 'bootstrap_doctrine.php';
}

bootstrap_doctrine.php

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

$paths = array("entities/");
$isDevMode = true;

// the connection configuration
$dbParams = array(
    'driver'   => 'pdo_mysql',
    'user'     => 'TEST',
    'password' => 'TEST',
    'dbname'   => 'test',
);

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

i checked if the /usr/share/pear/ is in php_include path. and it is ..

require_once 'System.php';
var_dump(class_exists('System', false));

it returns true:

but this one does return false:

use Doctrine\ORM\Tools\Setup;
var_dump(class_exists('Setup', false));

Am i doing something wrong?

best regards

like image 462
Laokoon Avatar asked Oct 16 '12 14:10

Laokoon


People also ask

What is the use of Doctrine in PHP?

The purpose of the Doctrine project is to build an equally powerful solution for the PHP language for high-load websites that have to maintain a constant flow of visitors. Doctrine ORM can be used to improve the performance of such websites.

What is entity manager in Doctrine?

The EntityManager is the central access point to ORM functionality. It can be used to find, persist, flush and remove entities.

What is Doctrine db?

The Doctrine Project is the home to several PHP libraries primarily focused on database storage and object mapping. The core projects are the Object Relational Mapper (ORM) and the Database Abstraction Layer (DBAL) it is built upon.


1 Answers

Yes it seems that autoloading is missing, the easiest way to autoload your classes is to use Composer which can also download dependencies such as Doctrine. To do this you have to get composer.phar installed either locally on your project root or globally on folder declared in your PATH system variable (/usr/local/bin/ is the recommended folder).

Then you have to edit a composer.json file in your project's root folder. In this file you will define your project's dependencies and the class pathes you want to be able to load.

{
    "require": {
        "Doctrine/ORM": "2.3.3"
    },
    "autoload": {
        "psr-0": {"MyProject\\Models\\": "src/models/"}
    }
}

Then all you have to do is to open a terminal from your project's root folder and type composer install. This will create a vendor folder containing all the downloaded dependencies. You will also get an autoload.php file in this vendor folder. You just have to include this autoloader in your php file to be able to use all the dependencies and all the namespaces you declared in the autoload section of composer.json.

You can get more infos about composer here : https://getcomposer.org/ You can also browse the available packages here : https://packagist.org/

Hope this will helps

like image 128
Sébastien Maloron Avatar answered Sep 28 '22 01:09

Sébastien Maloron