PHP Doctrine初学者:Doctrine\ORM\Tools\Setup未找到 [英] PHP Doctrine Beginner: Doctrine\ORM\Tools\Setup not found

查看:130
本文介绍了PHP Doctrine初学者:Doctrine\ORM\Tools\Setup未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个初学者与原则。我刚刚安装了pear + doctrine 2.3.3并想测试它。



测试原则我写了一个名为person的类

  / ** 
* @Entity
* /
类人
{
/ ** @ Id @Column(type =integer)@GeneratedValue * * /
private $ id;

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

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

//一些getter和setter ...
}

之后,我创建了我的bootstrap.php文件,bootstrep_doctrine.php和cli-config.php文件并运行命令:

  doctrine orm:模式工具:创建

正常工作



但是现在,当我想将我的bootstrap.php包含在普通php文件中时,要创建一个人,我会收到以下错误:

 致命错误:类'Doctrine\ORM\Tools\Setup'未找到
在/var/www/vms/bootstrap_doctrine.php第15行

该文件如下所示:

 <?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

 使用Doctrine\ORM\Tools\Setup; 
使用Doctrine\ORM\EntityManager;

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

//连接配置
$ dbParams = array(
'driver'=>'pdo_mysql',
'user'=>'TEST'
'password'=>'TEST',
'dbname'=>'test',
);

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

我检查了/ usr / share / pear /是否在php_include路径中。它是..

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

它返回true:



但这一个返回false:

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

我做错了什么?



最好的问候

解决方案

是的,似乎自动加载缺少,自动加载类的最简单的方法是使用Composer,它也可以下载依赖性,如教义。为此,您必须将composer.phar安装在您的项目根目录或全局安装在PATH系统变量中声明的文件夹( / usr / local / bin / 是推荐的文件夹)。



然后,您必须在项目的根文件夹中编辑 composer.json 文件。在此文件中,您将定义项目的依赖关系和您想要加载的类别。

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

然后你需要做的就是从项目的根文件夹打开一个终端,然后输入 composer install 。这将创建一个包含所有下载的依赖关系的供应商文件夹。您还将在此供应商文件夹中获取一个 autoload.php 文件。您只需将此自动加载器包含在您的php文件中即可使用您在 composer.json 的自动加载部分中声明的所有依赖项和所有命名空间。



您可以在这里获得更多关于作曲家的信息:
https:// getcomposer .org /
您还可以在这里浏览可用的软件包:
https:// packagist。 org /



希望这将有助于


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

解决方案

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

这篇关于PHP Doctrine初学者:Doctrine\ORM\Tools\Setup未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆