Search code examples
phppostgresqlherokupdosilex

Heroku can't find PDOServiceProvider


So I followed the tutorial provided by heroku on how to deploy a php app https://devcenter.heroku.com/articles/getting-started-with-php#introduction

Everything works fine, except when I get to setting up the database. At first I tried looking for different solutions online (i.e. Dokku deployed Silex can't find PdoServiceProvider), but not seens to work.

Right now, my code looks like this:

<?php
    use Csanquer\Silex\PdoServiceProvider\Provider\PDOServiceProvider;
    //use Silex\Application;

    $dbopts = parse_url(getenv('DATABASE_URL'));
    $pdo = new PDOServiceProvider('pdo');
    //$app = new Application();
    $app->register($pdo,
        array(
            'pdo.server' => array(
                'driver'   => 'pgsql',
                'user' => $dbopts["user"],
                'password' => $dbopts["pass"],
                'host' => $dbopts["host"],
                'port' => $dbopts["port"],
                'dbname' => ltrim($dbopts["path"],'/')
            )
        )
    );
?>

And I get the following message:

Uncaught Error: Class 'Csanquer\Silex\PdoServiceProvider\Provider\PDOServiceProvider' not found in /app/web/index.php:6

How can I fix it?


Solution

  • I hope you've used composer before, if not, let me know please. Try to install Doctrine DBAL using your composer:

    composer require "doctrine/dbal:~2.5"
    

    Once you've installed Doctrine you can use the following lines of code:

    $config = new \Doctrine\DBAL\Configuration();
    
    $connParams = array(
        'driver'   => #driver -> pdo_mysql,
        'dbname'   => #dbname,
        'host'     => #host -> localhost,
        'user'     => #user,
        'password' => #password,
        'charset'  => #charset -> utf8
    );
    $conn = \Doctrine\DBAL\DriverManager::getConnection($connParams, $config);
    

    You can now use something like:

    $conn->query(#SomeSQL)
    $conn->prepare(#SomeSQL)
    

    Or in the way you like to do it:

    $app->register(new Silex\Provider\DoctrineServiceProvider(), array(
        'db.options' => array(
            'driver'   => #driver -> pdo_mysql,
            'dbname'   => #dbname,
            'host'     => #host -> localhost,
            'user'     => #user,
            'password' => #password,
            'charset'  => #charset -> utf8
        ),
    ));
    

    You can now use something like:

    $app['db']->query(#someSQL);
    

    For more information on Doctrine DBAL consult the page here or here. Please let me know whether this has helped you! (By marking the question as completed or commenting on new errors).