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

独自空忆成欢 提交于 2019-12-07 03:46:30

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/12916985/php-doctrine-beginner-doctrine-orm-tools-setup-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!