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

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

to test the doctrine, I wrote a class called "man"

/** * @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 bootstrap.php, bootstrep_doctrine.php and cli-config.php files and ran the command:

 doctrine orm:schema-tool:create 

works great!

But now, when I want to include my bootstrap.php in a β€œregular” 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 is as 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 checks if / usr / share / pear / is in the php_include path. and this..

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

returns true:

but this returns false:

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

Am I doing something wrong?

Best wishes

+6
source share
1 answer

Yes, it seems that there is no autoload, the easiest way to autoload your classes is to use Composer, which can also load dependencies like Doctrine. To do this, you need to install the composer.phar component either locally in the project root directory or globally in the folder declared in your PATH system variable ( /usr/local/bin/ is the recommended folder).

Then you have to edit the composer.json file in the project root folder. In this file, you define the project dependencies and class templates that you want to load.

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

Then you only need to open the terminal from the project root folder and enter composer install . This will create a vendor folder containing all downloaded dependencies. You will also receive the autoload.php file in this provider folder. You just need to include this autoloader in your php file in order to be able to use all the dependencies and all namespaces that you specified in the composer.json startup section.

Here you can get additional information about the composer: https://getcomposer.org/ You can also view the available packages here: https://packagist.org/

Hope this helps

+3
source

Source: https://habr.com/ru/post/927854/


All Articles