2012-10-16 56 views
6

即時通訊與初學者的教條。我剛安裝了pear + doctrine 2.3.3並想測試它。PHP學說初學者:學說 ORM 工具安裝程序找不到

測試教義我寫了一個名爲 「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 ... 
} 

類之後,我做了我的bootstrap.php文件,bootstrep_doctrine.php和CLI-config.php文件的文件並運行命令:

doctrine orm:schema-tool:create 

工作正常!

但是現在,當我想在一個「正常」的php文件我bootstrap.php中,創建一個「人」,我收到以下錯誤:

Fatal error: Class 'Doctrine\ORM\Tools\Setup' not found 
in /var/www/vms/bootstrap_doctrine.php on line 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

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); 

我檢查/ usr/share/pear /是否在php_include路徑中。它是..

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

返回true:

但是這一次確實返回false:

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

我做錯什麼了嗎?

問候

+0

你忘了設定自動加載!你是如何安裝ORM的? – Ocramius

回答

3

是的,它似乎是自動加載缺失,自動加載最簡單的方法你的類是使用作曲家也可以下載相關性,如學說。要做到這一點,您必須將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/

希望這將有助於