Categories: DevelopmentPHP

Powerful database abstraction with Doctrine DBAL – Getting started

“Powerful database abstraction layer with many features for database schema introspection, schema management and PDO abstraction.”

The following will get you started, these offer the Doctrine\Common and Doctrine\DBAL namespaces.

  1. Doctrine DBAL
  2. Doctrine Common

BASIC SETUP

In the end your structure should look something like that:

includes/
includes/doctrine
includes/doctrine/lib
includes/doctrine/lib/Doctrine
includes/doctrine/lib/Doctrine/Common
includes/doctrine/lib/Doctrine/DBAL

The following will add a class loader, so that all the other classes will be autoloaded.

  1. use Doctrine\Common\ClassLoader;
  2.  
  3. require dirname(__FILE__).'/includes/doctrine/lib/Doctrine/Common/ClassLoader.php';
  4.  
  5. $classLoader = new ClassLoader('Doctrine',dirname(__FILE__).'/includes/doctrine/lib');
  6. $classLoader->register();

FIRST CONNECTION

This will setup your first connection to a MySQL database.

  1. $config = new \Doctrine\DBAL\Configuration();
  2.  
  3. $connectionParams = array(
  4.     'dbname' => 'my_db',
  5.     'user' => 'my_user',
  6.     'password' => 'my_pass',
  7.     'host' => 'localhost',
  8.     'driver' => 'pdo_mysql',
  9. );
  10. $conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);

FIRST QUERY

This will do a simple first query

  1. $sql = "SELECT * FROM some_data";
  2. $stmt = $conn->query($sql);
  3.  
  4. while ($row = $stmt->fetch()) {
  5.     echo $row['some_field'];
  6. }

DYNAMIC & PREPARED

DBAL gives us some nice options to prepare queries.

  1. $sql = "SELECT * FROM some_data WHERE id = ?";
  2. $stmt = $conn->prepare($sql);
  3. $stmt->bindValue(1, $id);
  4. $stmt->execute();

By using the bindValue the placeholder “?” is replaced. You can also use named parameters :)

  1. $sql = "SELECT * FROM some_data WHERE name = :name OR username = :name";
  2. $stmt = $conn->prepare($sql);
  3. $stmt->bindValue("name", $name);
  4. $stmt->execute();

More about this in the official documentation.

That was not too difficult ;)

Enjoy coding …

Alex

I am a full-stack developer. I love programming,  design and know my way around server architecture as well.  I would never feel complete, with one of these missing. I have a broad range of interests, that’s why I constantly dive into new technologies and expand my knowledge where ever required. Technologies are evolving fast and I enjoy using the latest. Apart from that, I am a peace loving guy who tries to have people around him that think the same.  I truly believe in the principle: “If you help someone, someone will help you, when you need it."

Recent Posts

B&B / Hotel Booking Solutions for WordPress | 2024

BOOKING SOLUTIONS 202x This is my take on a subset of booking, appointment, PMS or… Read More

4 weeks ago

WordPress Cron + WP-CLI + Ntfy

THE GOAL Create a system cron for WordPress, that is accessible and can be easily… Read More

2 months ago

2024 is here and now :)

2024, what's cooking? Slowly getting into the 2024 spirit. 3 projects coming to a close… Read More

4 months ago

2023 ends and whats next !

Short look back at 2023 This has been a busy and interesting year. I am… Read More

4 months ago

cubicFUSION Grid Tweaker – Elementor Grid made easy.

Elementor Pro provides grid containers as an experimental feature. The options provided are limited, when… Read More

5 months ago

Archaeology Travel Booth – Travel Innovation Summit 2023

Archaeology Travel is an online travel guide for people who enjoy exploring the world’s pasts.… Read More

6 months ago