2

Trying to add Doctrine DBAL into my own project to use it to access my db etc. I don't have composer and i never used it. This is what i am trying to do according to the docu:

use Doctrine\Common\ClassLoader;

class Connection

{
    var $connection;

//Constructor
public function __construct()
{
    require_once "doctrine/Common/ClassLoader.php";

    $classLoader = new ClassLoader('Doctrine', 'doctrine');
    $classLoader->register();
    $config = new Configuration();
    $connectionParams = array(
        'dbname' => 'mydb',
        'user' => 'root',
        'password' => "",
        'host' => 'localhost',
        'driver' => 'pdo_mysql',
    );


    $this->connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
}
}

This is taken from here: -http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html and: - http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/introduction.html

I have the Common and DBAL folder added into my project

My folder structure looks like this:

  1. root
    • doctrine
      • DBAL
      • Common
    • php stuff
    • index.php (where connection.php) is executed

So what happens is that i either get "Cannot find class XY" or something similar, based upon what i change on the code. I never am able to execute it as it should following the tutorial.

What am i doing wrong here?

I just want to have the connection object, where i can start doing my stuff like useing the query builder etc...

I am completely lost here...

UPDATE: Installed composer as requested and have this Code now:

use Doctrine\DBAL\Configuration;

class Connection
{
    var $connection;

    //Constructor
    public function __construct()
    {
        $config = new Configuration();
        $connectionParams = array(
            'url' => 'mysql://root:secret@localhost/mydb',
        );
        $this->connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
    }

Which is the 2nd code example in my 1st link. Tells me " Class 'Doctrine\DBAL\Configuration' not found ". Funny thing is, that IntelliJ can perfectly autocomplete the path (suggests me Configuration when finishing DBAL in the path) but PHP doesn't find it. If i remove the new Configuration PHP just tells me, that it doesn't find the DriverManager...

I installed it correctly via composer though, at least composer tells me it is installed correctly now (Where does composer save the libs?)

BoA456
  • 341
  • 2
  • 15
  • 2
    _I don't have composer and i never used it._ Change this, you won't regret it! – baao Aug 09 '15 at 21:00
  • I totally agree with @michael: Composer is an easy way to manage external libraries: [try it](https://getcomposer.org/doc/00-intro.md) and you'll never go back ;) –  Aug 09 '15 at 21:03
  • Yes, i'm sure that is correct. However for this project i just want to get this working now and actually start using the Query Builder... nothing else... – BoA456 Aug 09 '15 at 21:18
  • @michael: Ok, just installed composer. Installed doctrine/dbal, it says i installed / updated it successfully. Now: How the hell do i use doctrine afer importing it with composer? Copying the example code just doesnt work for me... – BoA456 Aug 09 '15 at 21:52
  • Well, if it is setup correctly, you can simply `use Namespace\Class' dbal in your file. Can you please update your question with the code you are having now? I will have a look then! – baao Aug 09 '15 at 21:53
  • @michael: Done! Thanks! – BoA456 Aug 09 '15 at 22:00
  • Can you please run `composer dump-autoload` in the directory the files are in, and please try if it works with the autloader as I posted below – baao Aug 09 '15 at 22:01

2 Answers2

2

You now need to require composers autoload file.

require __DIR__.'/vendor/autoload.php';
use Doctrine\DBAL\Configuration;

class Connection
{
    var $connection;

    //Constructor
    public function __construct()
    {
        $config = new Configuration();
        $connectionParams = array(
            'url' => 'mysql://root:secret@localhost/mydb',
        );
        $this->connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
    }

Please note, depending on your directory structure, the autoload file might be somewhere else, but usually this should work.

baao
  • 71,625
  • 17
  • 143
  • 203
  • But I'm sure you will love composer once you are more comfortable with using it. It's a realy nice tool! @BoA456 – baao Aug 09 '15 at 22:18
0

Pay attention to the use of namespaces: if the Doctrine namespace for its loader is Doctrine\Common\ClassLoader, you have to put the files inside the Doctrine\Common folder ("Doctrine" with a capital "D").

See the code snippet shown inside the Introduction chapter of the Doctrine DBAL documentations.

  • Ok, did that now. Thanks for clearing that up. However PHP is now stating that "Class 'Doctrine\DBAL\Configuration' not found", although i added use Doctrine\DBAL\Configuration ... – BoA456 Aug 09 '15 at 21:18