Rewrite installation and configuration of Tools chapter.
This commit is contained in:
parent
1d5597917b
commit
7b4349a9ce
@ -12,15 +12,11 @@ Installation
|
|||||||
~~~~~~~~~~~~
|
~~~~~~~~~~~~
|
||||||
|
|
||||||
If you installed Doctrine 2 through PEAR, the ``doctrine`` command
|
If you installed Doctrine 2 through PEAR, the ``doctrine`` command
|
||||||
line tool should already be available to you.
|
line tool should already be available to you. Y
|
||||||
|
|
||||||
If you use Doctrine through SVN or a release package you need to
|
In any other case you should create a project specific doctrine command
|
||||||
copy the ``doctrine`` and ``doctrine.php`` files from the
|
on your own. This is a combination of the PEAR ``doctrine`` commands
|
||||||
``tools/sandbox`` or ``bin`` folder, respectively, to a location of
|
code and some of your own bootstrap code.
|
||||||
your choice, for example a ``tools`` folder of your project. You
|
|
||||||
probably need to edit ``doctrine.php`` to adjust some paths to the
|
|
||||||
new environment, most importantly the first line that includes the
|
|
||||||
``Doctrine\Common\ClassLoader``.
|
|
||||||
|
|
||||||
Getting Help
|
Getting Help
|
||||||
~~~~~~~~~~~~
|
~~~~~~~~~~~~
|
||||||
@ -52,21 +48,23 @@ In general the required code looks like this:
|
|||||||
Doctrine\ORM\Tools\Console\ConsoleRunner::addCommands($cli);
|
Doctrine\ORM\Tools\Console\ConsoleRunner::addCommands($cli);
|
||||||
$cli->run();
|
$cli->run();
|
||||||
|
|
||||||
Configuration
|
Configuration (PEAR)
|
||||||
~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
Whenever the ``doctrine`` command line tool is invoked, it can
|
Whenever the ``doctrine`` command line tool is invoked, it can
|
||||||
access alls Commands that were registered by developer. There is no
|
access alls Commands that were registered by developer. There is no
|
||||||
auto-detection mechanism at work. The ``bin\doctrine.php`` file
|
auto-detection mechanism at work. The Doctrine binary
|
||||||
already registers all the commands that currently ship with
|
already registers all the commands that currently ship with
|
||||||
Doctrine DBAL and ORM. If you want to use additional commands you
|
Doctrine DBAL and ORM. If you want to use additional commands you
|
||||||
have to register them yourself.
|
have to register them yourself.
|
||||||
|
|
||||||
All the commands of the Doctrine Console require either the ``db``
|
All the commands of the Doctrine Console require access to the EntityManager
|
||||||
|
or DBAL Connection. You have to inject them into the console application
|
||||||
|
using so called Helper-Sets. This requires either the ``db``
|
||||||
or the ``em`` helpers to be defined in order to work correctly.
|
or the ``em`` helpers to be defined in order to work correctly.
|
||||||
Doctrine Console requires the definition of a HelperSet that is the
|
|
||||||
DI tool to be injected in the Console. In case of a project that is
|
Whenever you invoke the Doctrine binary the current folder is searched for a
|
||||||
dealing exclusively with DBAL, the ConnectionHelper is required:
|
``cli-config.php`` file. This file contains the project specific configuration:
|
||||||
|
|
||||||
.. code-block:: php
|
.. code-block:: php
|
||||||
|
|
||||||
@ -90,49 +88,108 @@ required:
|
|||||||
The HelperSet instance has to be generated in a separate file (i.e.
|
The HelperSet instance has to be generated in a separate file (i.e.
|
||||||
``cli-config.php``) that contains typical Doctrine bootstrap code
|
``cli-config.php``) that contains typical Doctrine bootstrap code
|
||||||
and predefines the needed HelperSet attributes mentioned above. A
|
and predefines the needed HelperSet attributes mentioned above. A
|
||||||
typical ``cli-config.php`` file looks as follows:
|
sample ``cli-config.php`` file looks as follows:
|
||||||
|
|
||||||
.. code-block:: php
|
.. code-block:: php
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
require_once __DIR__ . '/../../lib/Doctrine/Common/ClassLoader.php';
|
// cli-config.php
|
||||||
|
use Doctrine\ORM\Tools\Setup;
|
||||||
$classLoader = new \Doctrine\Common\ClassLoader('Entities', __DIR__);
|
use Doctrine\ORM\EntityManager;
|
||||||
$classLoader->register();
|
|
||||||
|
require 'Doctrine/ORM/Tools/Setup.php';
|
||||||
$classLoader = new \Doctrine\Common\ClassLoader('Proxies', __DIR__);
|
|
||||||
$classLoader->register();
|
Doctrine\ORM\Tools\Setup::registerAutoloadPEAR();
|
||||||
|
|
||||||
$config = new \Doctrine\ORM\Configuration();
|
$paths = array("/path/to/entities-or-mapping-files");
|
||||||
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
|
$isDevMode = false;
|
||||||
$config->setProxyDir(__DIR__ . '/Proxies');
|
|
||||||
$config->setProxyNamespace('Proxies');
|
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
|
||||||
|
$em = EntityManager::create($dbParams, $config);
|
||||||
$connectionOptions = array(
|
|
||||||
'driver' => 'pdo_sqlite',
|
|
||||||
'path' => 'database.sqlite'
|
|
||||||
);
|
|
||||||
|
|
||||||
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
|
|
||||||
|
|
||||||
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
|
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
|
||||||
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
|
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
|
||||||
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
|
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
|
||||||
));
|
));
|
||||||
|
|
||||||
It is important to define a correct HelperSet that doctrine.php
|
It is important to define a correct HelperSet that Doctrine binary
|
||||||
script will ultimately use. The Doctrine Binary will automatically
|
script will ultimately use. The Doctrine Binary will automatically
|
||||||
find the first instance of HelperSet in the global variable
|
find the first instance of HelperSet in the global variable
|
||||||
namespace and use this.
|
namespace and use this.
|
||||||
|
|
||||||
You can also add your own commands on-top of the Doctrine supported
|
|
||||||
tools. To include a new command on Doctrine Console, you need to
|
.. note::
|
||||||
do:
|
|
||||||
|
You have to adjust this snippet for your specific application or framework
|
||||||
|
and use their facilities to access the Doctrine EntityManager and
|
||||||
|
Connection Resources.
|
||||||
|
|
||||||
|
Configuration (Non-PEAR)
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
If you do not use a PEAR installation of Doctrine you have to define your own
|
||||||
|
Doctrine binary. Put this file into the application root and invoke it from
|
||||||
|
there whenever you want to access the Doctrine console.
|
||||||
|
|
||||||
.. code-block:: php
|
.. code-block:: php
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
$cli->addCommand(new \MyProject\Tools\Console\Commands\MyCustomCommand());
|
// doctrine.php - Put in your application root
|
||||||
|
|
||||||
|
use Doctrine\ORM\Tools\Setup;
|
||||||
|
use Doctrine\ORM\EntityManager;
|
||||||
|
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
|
||||||
|
use Doctrine\DBAL\Tools\Console\Helper\EntityManagerHelper;
|
||||||
|
use Doctrine\ORM\Tools\Console\ConsoleRunner;
|
||||||
|
use Symfony\Component\Console\Helper\HelperSet;
|
||||||
|
|
||||||
|
$lib = "/path/to/doctrine2-orm/lib";
|
||||||
|
require $lib . '/Doctrine/ORM/Tools/Setup.php';
|
||||||
|
Setup::registerAutoloadDirectory($lib);
|
||||||
|
|
||||||
|
$paths = array("/path/to/entities-or-mapping-files");
|
||||||
|
$isDevMode = false;
|
||||||
|
|
||||||
|
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
|
||||||
|
$em = EntityManager::create($dbParams, $config);
|
||||||
|
|
||||||
|
$helperSet = new HelperSet(array(
|
||||||
|
'db' => new ConnectionHelper($em->getConnection()),
|
||||||
|
'em' => new EntityManagerHelper($em)
|
||||||
|
));
|
||||||
|
|
||||||
|
ConsoleRunner::run($helperSet);
|
||||||
|
|
||||||
|
Adding own commands
|
||||||
|
~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
You can also add your own commands on-top of the Doctrine supported
|
||||||
|
tools if you are using a manually built (Non-PEAR) binary.
|
||||||
|
|
||||||
|
To include a new command on Doctrine Console, you need to do modify the
|
||||||
|
``doctrine.php`` file a little:
|
||||||
|
|
||||||
|
.. code-block:: php
|
||||||
|
|
||||||
|
<?php
|
||||||
|
// doctrine.php
|
||||||
|
use Symfony\Component\Console\Helper\Application;
|
||||||
|
|
||||||
|
// as before ...
|
||||||
|
|
||||||
|
// replace the ConsoleRunner::run() statement with:
|
||||||
|
$cli = new Application('Doctrine Command Line Interface', \Doctrine\ORM\Version::VERSION);
|
||||||
|
$cli->setCatchExceptions(true);
|
||||||
|
$cli->setHelperSet($helperSet);
|
||||||
|
|
||||||
|
// Register All Doctrine Commands
|
||||||
|
ConsoleRunner::addCommands($cli);
|
||||||
|
|
||||||
|
// Register your own command
|
||||||
|
$cli->addCommand(new \MyProject\Tools\Console\Commands\MyCustomCommand);
|
||||||
|
|
||||||
|
// Runs console application
|
||||||
|
$cli->run();
|
||||||
|
|
||||||
Additionally, include multiple commands (and overriding previously
|
Additionally, include multiple commands (and overriding previously
|
||||||
defined ones) is possible through the command:
|
defined ones) is possible through the command:
|
||||||
@ -140,6 +197,7 @@ defined ones) is possible through the command:
|
|||||||
.. code-block:: php
|
.. code-block:: php
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
$cli->addCommands(array(
|
$cli->addCommands(array(
|
||||||
new \MyProject\Tools\Console\Commands\MyCustomCommand(),
|
new \MyProject\Tools\Console\Commands\MyCustomCommand(),
|
||||||
new \MyProject\Tools\Console\Commands\SomethingCommand(),
|
new \MyProject\Tools\Console\Commands\SomethingCommand(),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user