3

我有这个文件夹结构

[-] myapp/
    [+] app/
    [-] lib/
        [-] vendor/
            [-] doctrine/
                [-] Doctrine/
                        Common/
                        DBAL/
                        Symfony/
                [+] bin/
    [-] sites/
        [-] default/
            [-] test/
                    test-doctrine-dbal.php

我在文档中尝试了代码

    <?php

use Doctrine\Common\ClassLoader;

require dirname(__FILE__).'/../../../lib/vendor/doctrine/Doctrine/Common/ClassLoader.php';

$classLoader = new ClassLoader('Doctrine', dirname(__FILE__).'/../../../lib/vendor/doctrine/Doctrine/DBAL/DriverManager.php');
$classLoader->register();

$config = new \Doctrine\DBAL\Configuration();
$connectionParams = array(
    'dbname' => 'cdcol',
    'user' => 'root',
    'password' => '',
    'host' => 'localhost',
    'driver' => 'pdo_mysql',
);
$conn = DriverManager::getConnection($connectionParams, $config);

$sql = "SELECT * FROM cds";
$stmt = $conn->query($sql);
while ($row = $stmt->fetch()) {
    echo $row['titel'];
}
?>

我收到警告:

Warning: require(D:\xampp\htdocs\myphp\sites\default\test/../../../lib/vendor/doctrine/Doctrine/DBAL/DriverManager.php\Doctrine\DBAL\Configuration.php) [function.require]: failed to open stream: No such file or directory in D:\xampp\htdocs\myphp\lib\vendor\doctrine\Doctrine\Common\ClassLoader.php on line 148

还有一个错误:

Fatal error: require() [function.require]: Failed opening required 'D:\xampp\htdocs\myphp\sites\default\test/../../../lib/vendor/doctrine/Doctrine/DBAL/DriverManager.php\Doctrine\DBAL\Configuration.php' (include_path='.;D:\xampp\php\PEAR') in D:\xampp\htdocs\myphp\lib\vendor\doctrine\Doctrine\Common\ClassLoader.php on line 148

我对 PHP 命名空间了解不多。阅读 PHP 手册中的 PHP 命名空间仍然无法解决问题。如果我想将 Doctrine DBAL 与该目录结构一起使用,该代码是否正确?

4

3 回答 3

2

您需要将 Doctrine 库路径添加到include_path. 请参阅get_include_pathset_include_pathPHP 函数。

于 2011-07-18T07:29:12.733 回答
1

这对我来说看起来不对(来自警告)

D:\xampp\htdocs\myphp\sites\default\test/../../../lib/vendor/doctrine/Doctrine/DBAL/DriverManager.php\Doctrine\DBAL\Configuration.php

我对php不太了解,但我认为文件路径无法解析。现在,尝试删除dirname( _ __FILE_ _ _)并在其中硬编码路径。

于 2011-07-18T15:05:00.637 回答
0

您的构造函数的第二个参数ClassLoader无效:

$classLoader = new ClassLoader('Doctrine', dirname(_ FILE _).'/../../../lib/vendor/doctrine/Doctrine/DBAL/DriverManager.php' );

第二个参数应该是include pathDoctrine 目录的,而不应该是文件。

因此,正确的行是:

$classLoader = new ClassLoader('Doctrine', '/../../../lib/vendor/doctrine/' );

于 2013-11-15T12:31:08.060 回答