我有这个文件夹结构
[-] 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与该目录结构一起使用,该代码是否正确?
答案 0 :(得分:2)
您需要将Doctrine库路径添加到include_path
。请参阅get_include_path
和set_include_path
PHP函数。
答案 1 :(得分:1)
这对我来说是错误的(来自警告)
D:\xampp\htdocs\myphp\sites\default\test/../../../lib/vendor/doctrine/Doctrine/DBAL/DriverManager.php\Doctrine\DBAL\Configuration.php
我对PHP知之甚少,但我不认为文件路径可以解决。现在,请尝试删除 dirname( _ __ FILE_ _ _),然后只修改其中的路径。
答案 2 :(得分:0)
ClassLoader
构造函数的第二个参数无效:
$ classLoader = new ClassLoader('Doctrine', dirname(_ FILE _)。'/ .. / .. / .. / lib / vendor / doctrine / Doctrine / DBAL /DriverManager.php'强>);
第二个参数应该是Doctrine目录的include path
,不应该是文件。
因此,正确的行是:
$ classLoader = new ClassLoader('Doctrine','/../../../ lib / vendor / doctrine /');