When just wanting to take Doctrine for a spin it is not that straight forwarded on how to set up a project (even though it should be). In this post i will explain how to setup the folders and files to have Doctrine's functionality working. Also if you want to integrate it with your own application you can follow this post.

Setup

Composer

We are going to use composer here because it's a really convenient package manager, which a php project can't really go without these days. Also we will be following the PSR-0 standard for folders and namespaces.

Start with creating a project folder with a composer.json file containing:

{
    "require": {
        "doctrine/orm": "~2.4.0"
    },
    "autoload": {
        "psr-0": { "": "src/" }
    },
    "config": {
        "bin-dir": "bin"
    }
}

Yes .. that's it. This package has has dependencies on other Doctrine components so you will be pulling in things like a Database Abstraction Layer (DBAL) as well. The bin dir has been setup for easy access to the Doctrine CLI tool.

Files and folders

After a composer install some extra folders and files have to be created. Create the files, but leave them empty for now.

vendor/ # automatically maintained by composer
src/ # for our PSR-0 classes
src/Vendor/Entity/ # Here we will store our Doctrine models. Rename Vendor to your liking
composer.json # we already had this one
bootstrap.php # configuration & initialization
cli-config.php # php file required by doctrine to work from the command line
app.php # the main application

bootstrap

The bootstrap file will setup the database connection and the EntityManager to work with your models. This example reads the model metadata from annotations and uses a PostgreSQL database.

<?php
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;

require_once './vendor/autoload.php';

$dbParams = array(
  'driver' => 'pdo_pgsql',
  'host' => 'localhost',
  'port' => 5432,
  'user' => 'postgres',
  'password' => '',
  'dbname' => '',
  'charset' => 'UTF8'
);

$config = Setup::createConfiguration(true);
$driver = new AnnotationDriver(new AnnotationReader(), array(__DIR__.'/src/Doctrine/Entity'));

AnnotationRegistry::registerLoader('class_exists');
$config->setMetadataDriverImpl($driver);

/*
Just for convenience, normally you would not put this in a bootstrap file
*/
$em = EntityManager::create($dbParams, $config);
$conn = $em->getConnection();

The command line tool

The command line tool requires a file called cli-config.php which will look like this

<?php
use Doctrine\ORM\Tools\Console\ConsoleRunner;

require_once 'bootstrap.php';

return ConsoleRunner::createHelperSet($em);

To start using the tool type:

  php bin/doctrine --help

If that is still too long for you, setup a script, doctrine

#!/bin/sh
bin/doctrine "$@"

Or in windows doctrine.bat

@call bin\doctrine.bat %*

Usage

Before we dive into anything let's create a simple model user.php

<?php
namespace Vendor\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table
 */
class User
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string")
     */
    private $name;
}

Using the command line tool

Two commands are particularly useful in rapid prototyping.

  1. Generating class methods with ./doctrine orm:generate:entities src
  2. Creating the schema in the datbase with ./doctrine orm:schema-tool:update --force

Be sure to check out the full list of commands.

Working with the models

Below you can find an example for app.php that just shows you how to create a new user in the database.

<?php
use Vendor\Entity\User;

$user = new User();
$user->setName('John');

$em->persist($user);
$em->flush();