4. Doctrine command line interface

Doctrine allows to manage the database schema through a simple command-line interface. However, we do not get a ready script, but rather a library that must be configured properly in order to create a complete script. We must create a new file as /tools/doctrine.php and fill it with the following code:

$paths = array(
    'data_fixtures_path' => '../doctrine/fixtures',
    'models_path' => '../models',
    'migrations_path' => '../doctrine/migrations',
    'sql_path' => '../doctrine/sql',
    'yaml_schema_path' => '../doctrine/schema'
// Set the autoloader
spl_autoload_register(array('Doctrine', 'autoload'));
// Load the configuration
// Doctrine initialization
$conn = Doctrine_Manager::connection($config['database']['dsn']);
foreach($config['database']['attributes'] as $attribute_name => $attribute_value)
    $conn->setAttribute($attribute_name, $attribute_value);
$conn->setAttribute(Doctrine::ATTR_QUOTE_IDENTIFIER, true);
$conn->setAttribute(Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(Doctrine::ATTR_EXPORT, Doctrine::EXPORT_ALL);
$manager->setAttribute(Doctrine::ATTR_MODEL_LOADING, Doctrine::MODEL_LOADING_CONSERVATIVE);
// Run the command-line interface
$cli = new Doctrine_Cli($paths);

In the first lines we specify the paths to the Doctrine schema directories etc. Then we initialize the autoloader and load the connection configuration. Using those data we have to connect to the database, set some settings and finally – execute the CLI library. The database connection is established with a static method Doctrine_Manager::connection() which takes one argument called DSN. It contains all the information on the connection arguments and looks like this:


The example above shows its syntax for MySQL databases. Other engines may require different forms that respect system-specific settings. Below, you can find the used Doctrine options explained:

  1. Doctrine::ATTR_MODEL_LOADING – specifies the model loading policy. "Conservative" value means lazy-loading. The models are loaded when they are needed.

  2. Doctrine::ATTR_EXPORT – what to export to the database engine. EXPORT_ALL guarantees us that Doctrine exports also the foreign key and relationship information.

  3. Doctrine::ATTR_QUOTE_IDENTIFIER – allows to use identifiers that are reserved SQL keywords. They will be automatically escaped.

  4. Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE – enables automatic accessor overriding. We will discuss it later.

To make it work, we need to create a confiuguration file: /config.php:

$config = array(
    'database' => array(
        'dsn' => 'mysql://user:password@host/dbname',
        'charset' => 'utf8',
        'attributes' => array(
            'use_native_enum' => true

Now we can run the command line in our operating system. Ensure that your OS knows where PHP is located by entering:

php –version

If you get the information about PHP version, everything is all right. In any other case, you need to add the path to the PHP executable to your PATH environment variable. Once we know that we have PHP, we can switch to the directory with our photo gallery:

cd /path/to/photogallery/tools
php doctrine.php build-all-reload

The build-all-reload command removes and uploads once again the schema to the database engine using the settings from the YAML files. Furthermore, it generates the PHP models. As you can see, there should appear some new PHP files in /models directory. They are the PHP models that we are going to manipulate the data in the database. Our database structure is ready and now we can go to the next step – extending our models with some extra custom features.