3

I have this folder structure

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

And I try the code at documentation

    <?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'];
}
?>

And I get warning:

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

And an error:

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

I don't know much about PHP namespace. Reading about PHP namespace in PHP manual still cannot resolve the problem. Is that code correct if I want to use Doctrine DBAL with that directory structure ?

Permana
  • 1,972
  • 2
  • 33
  • 51

3 Answers3

2

You need to add the Doctrine library path to the include_path. See get_include_path and set_include_path PHP functions.

takeshin
  • 49,108
  • 32
  • 120
  • 164
  • 1
    I try add this : set_include_path(realpath(dirname(_ _ FILE _ _).'/../../../lib/vendor/doctrine/Doctrine/')); but still return the same error. I put space in _ and _ because double _ not shown up in SO – Permana Jul 18 '11 at 08:56
  • @Permana Check whether `realpath` returns string or boolean. You probably have too many/few dots in the relative path or some file permission issues. – takeshin Jul 19 '11 at 08:04
  • Try to `require` the file manually providing the correct path. Check the file using `is_readable`. Maybe it's a file permission issue. – takeshin Jul 29 '11 at 16:52
1

This looks wrong to me (from the warning)

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

I don't know that much about php but I don't think that file path can resolve. For now, try removing dirname(___FILE___) and just hardcoding the path in there.

i8abug
  • 1,692
  • 3
  • 19
  • 31
  • I have try without dirname(__FILE__) and still error with same message. I also try adding realpath(dirname(__FILE__).'/../../path') and still error with same message – Permana Jul 19 '11 at 02:14
  • when you try hardcoding the path, what is the path that you used? (ie, save the entire path to a string, echo the string so we can see what it is, and include it using require($myPath);). If that isn't the issue, are you using a name space? I ran into some issues with Doctrine when I used my own namespaces with doctrine. – i8abug Jul 19 '11 at 08:25
  • the require function is ok. I try to run only the require function and it return no error (I set error_reporting(E_ALL)). I'm not using namespace, but doctrine did. – Permana Jul 29 '11 at 07:11
0

Your 2nd parameter of ClassLoader constructor is not valid:

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

The 2nd parameter should be the include path of the Doctrine directory and shouldn't be a file.

Therefore, the correct line is:

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