Doubt about the Doctrine dbal project structure

Asked

Viewed 198 times

1

I read the Documentation the only thing I didn’t understand was what his structure is like.

Explain:

My file composer.json at the root of the project

{
 "require": {
    "doctrine/dbal": "2.4.*"
  }
}

When executing the command:

php composer.phar install

it downloads the dependencies generating the folder vendor ok. But there are many files that according to the documentation are unnecessary. See in the image my project: Meu projeto

I want to understand the structure of this project so I’ve been reading the only important part is the \lib\DBAL the others don’t. That’s right. And in relation to the project I wanted to use it like this. Have a folder \class and inside put the files both of doctrine\dbal as to classes bootstrap.php and crud (would be crud.php with the functions of CRUD and a require(bootstrap.php) Bootstrap is where the connection to the bank would be). And outside the folder class(project root) make function calls on my pages. It’s how I do in a normal project.

I’m asking this question because I haven’t found anything on the internet that talks about this subject or in the documentation. I’m starting at doctrine\dbal, I’m sorry if I’m being incomprehensible.

1 answer

2


You’re doing this almost the right way.

When you install a dependency on your project, it often has other dependencies. In the case of doctrine/dbal, as you can see in packagist (or directly, on its own repository), one of its dependencies is the package doctrine/common.

Doctrine is already a library that has several years of maturity, so it is somewhat obvious that it does not consist of just one or a few files. However, with dependency management getting easier and easier, this shouldn’t be a problem, since the dependencies are in the folder vendor/ and your code is at the root of the project (in general, the folder vendor/ should not be commited, and yes, your files composer.json and composer.lock).

I did a little project here, and the structure went like this:

Composer.json:

{
    "name": "root/teste-dbal",
    "require": {
        "doctrine/dbal": "2.5.x-dev"
    }
}

autoload.php:

<?php

require_once(__DIR__ . '/vendor/autoload.php');

bootstrap.php

<?php

require_once(__DIR__ . '/autoload.php');

// prepara a conexao
$config = new \Doctrine\DBAL\Configuration();
$connectionParameters = [
    'dbname'      => 'cms',
    'user'        => 'root',
    'password'    => '',
    'host'        => 'localhost',
    'driver'      => 'pdo_mysql',
    'unix_socket' => '/tmp/mysql.sock'
];
$connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParameters, $config);

crud.php

<?php

require_once(__DIR__ . '/bootstrap.php');

// executa a query
$sql = "SELECT * FROM users";
$statement = $connection->prepare($sql);
$statement->execute();
$users = $statement->fetchAll();

If any more questions come up, just say the word. :)

  • Blz. I’m trying to do something to impregnate this knowledge in it, Thanks, doubts put yes.

  • Did it work out? If so, don’t forget to validate the answer. :)

  • I am testing yes. Thanks I believe it will work, I will press it. How validate the answer here? I do not know how to validate.

  • Just dial with an "ok" near the beginning of the answer, on the left side.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.