Permana
Permana

Reputation: 1991

How to use Doctrine DBAL?

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 ?

Upvotes: 3

Views: 5049

Answers (3)

user2996193
user2996193

Reputation: 1

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

Upvotes: 0

i8abug
i8abug

Reputation: 1682

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.

Upvotes: 1

takeshin
takeshin

Reputation: 50648

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

Upvotes: 2

Related Questions