Setup Doctrine CLI

Posted by | February 24, 2012 | PHP, Sysadmin, Zend | No Comments

In the previous article have showed you how to integrate the almighty Doctrine ORM in Zend. But I haven’t mentioned nothing about the magic of this wonderful framework CLI.

I suppose that you have already done all the required magic incantations to integrate Doctrine in Zend, now what you are supposed to do next is to create in your Zend project a folder called bin and add doctrine.php file with the following content :

define('APPLICATION_ENV', 'development');
 
define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
 
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
 
// Doctrine and Symfony Classes
require_once 'Doctrine/Common/ClassLoader.php';
 
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine', APPLICATION_PATH . '/../library');
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Symfony', APPLICATION_PATH . '/../library/Doctrine');
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Entities', APPLICATION_PATH . '/models');
$classLoader->setNamespaceSeparator('_');
$classLoader->register();
 
// Zend Components
require_once 'Zend/Application.php';
 
// Create application
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
 
// bootstrap doctrine
$application->getBootstrap()->bootstrap('doctrine');
 
$em = $application->getBootstrap()->getResource('doctrine');
 
// generate the Doctrine HelperSet
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));
 
\Doctrine\ORM\Tools\Console\ConsoleRunner::run($helperSet);

Now you have the php file that will provide you basic functionalities for Doctrine CLI, next add a file called doctrine (wihtout extension) containing

#!/usr/bin/env php

include('doctrine.php');

open the almighty console and mark this as executable (using chmod 777) and now you are done open the console and type ./doctrine, if everything is OK you should get something like:

Doctrine Command Line Interface version 2.1.0
 
Usage:
[options] command [arguments]
 
Options:
--help           -h Display this help message.
--quiet          -q Do not output any message.
--verbose        -v Increase verbosity of messages.
--version        -V Display this program version.
--ansi              Force ANSI output.
--no-ansi           Disable ANSI output.
--no-interaction -n Do not ask any interactive question.
 
Available commands:
help                             Displays help for a command
list                             Lists commands
dbal
dbal:import                      Import SQL file(s) directly to Database.
dbal:run-sql                     Executes arbitrary SQL directly from the command line.
orm
orm:clear-cache:metadata         Clear all metadata cache of the various cache drivers.
orm:clear-cache:query            Clear all query cache of the various cache drivers.
orm:clear-cache:result           Clear result cache of the various cache drivers.
orm:convert-d1-schema            Converts Doctrine 1.X schema into a Doctrine 2.X schema.
orm:convert-mapping              Convert mapping information between supported formats.
orm:ensure-production-settings   Verify that Doctrine is properly configured for a production environment.
orm:generate-entities            Generate entity classes and method stubs from your mapping information.
orm:generate-proxies             Generates proxy classes for entity classes.
orm:generate-repositories        Generate repository classes from your mapping information.
orm:info                         Show basic information about all mapped entities
orm:run-dql                      Executes arbitrary DQL directly from the command line.
orm:schema-tool:create           Processes the schema and either create it directly on EntityManager Storage Connection or generate the SQL output.
orm:schema-tool:drop             Drop the complete database schema of EntityManager Storage Connection or generate the corresponding SQL output.
orm:schema-tool:update           Executes (or dumps) the SQL needed to update the database schema to match the current mapping metadata.
orm:validate-schema              Validate that the mapping files.

So this is it,

About Andrei Tara

Andrei is a developer/geek who loves creating high quality and efficient applications, always following the best practices in software engineering. He loves the challenge of learning new, exciting things and playing with new languages, frameworks, and tools.

Leave a Reply