我有这个文件夹结构
[-] 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 与该目录结构一起使用,该代码是否正确?