2013-03-16 23:36:09 +04:00
Installation and Configuration
==============================
2010-11-02 00:03:50 +03:00
2013-03-16 23:36:09 +04:00
Doctrine can be installed with `Composer <http://www.getcomposer.org> `_ . For
older versions we still have `PEAR packages
<http://pear.doctrine-project.org> `_.
2010-11-01 23:16:12 +03:00
2013-03-16 23:36:09 +04:00
Define the following requirement in your `` composer.json `` file:
::
{
"require": {
"doctrine/orm": "*"
}
}
Then call `` composer install `` from your command line. If you don't know
how Composer works, check out their `Getting Started
<http://getcomposer.org/doc/00-intro.md> `_ to set up.
2012-06-16 13:57:56 +04:00
2010-11-01 23:16:12 +03:00
Class loading
2012-06-16 13:57:56 +04:00
-------------
Autoloading is taken care of by Composer. You just have to include the composer autoload file in your project:
.. code-block :: php
<?php
2013-03-16 23:36:09 +04:00
// bootstrap.php
// Include Composer Autoload (relative to project root).
2012-06-16 13:57:56 +04:00
require_once "vendor/autoload.php";
2010-11-01 23:16:12 +03:00
Obtaining an EntityManager
2012-06-16 13:57:56 +04:00
--------------------------
2010-11-01 23:16:12 +03:00
Once you have prepared the class loading, you acquire an
*EntityManager* instance. The EntityManager class is the primary
access point to ORM functionality provided by Doctrine.
2012-06-16 13:57:56 +04:00
.. code-block :: php
<?php
2013-03-16 23:36:09 +04:00
// bootstrap.php
require_once "vendor/autoload.php";
2012-06-16 13:57:56 +04:00
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
$paths = array("/path/to/entities-or-mapping-files");
$isDevMode = false;
2012-06-18 20:43:10 +04:00
// the connection configuration
2012-06-18 19:42:49 +04:00
$dbParams = array(
'driver' => 'pdo_mysql',
'user' => 'root',
'password' => '',
'dbname' => 'foo',
);
2012-06-16 13:57:56 +04:00
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
2013-03-16 23:36:09 +04:00
$entityManager = EntityManager::create($dbParams, $config);
2012-06-16 13:57:56 +04:00
2013-03-16 23:36:09 +04:00
Or if you prefer XML:
2010-11-01 23:16:12 +03:00
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2013-03-16 23:36:09 +04:00
$config = Setup::createXMLMetadataConfiguration($paths, $isDevMode);
$entityManager = EntityManager::create($dbParams, $config);
2010-11-01 23:16:12 +03:00
2013-03-16 23:36:09 +04:00
Or if you prefer YAML:
2010-11-01 23:16:12 +03:00
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
2013-03-16 23:36:09 +04:00
$config = Setup::createYAMLMetadataConfiguration($paths, $isDevMode);
$entityManager = EntityManager::create($dbParams, $config);
2010-11-01 23:16:12 +03:00
2013-03-16 23:36:09 +04:00
Inside the `` Setup `` methods several assumptions are made:
2010-11-01 23:16:12 +03:00
2013-03-16 23:36:09 +04:00
- If `$devMode` is true always use an `` ArrayCache `` and set `` setAutoGenerateProxyClasses(true) `` .
- If `$devMode` is false, check for Caches in the order APC, Xcache, Memcache (127.0.0.1:11211), Redis (127.0.0.1:6379) unless `$cache` is passed as fourth argument.
- If `$devMode` is false, set `` setAutoGenerateProxyClasses(false) ``
- If third argument `$proxyDir` is not set, use the systems temporary directory.
2010-11-01 23:16:12 +03:00
2013-03-16 23:36:09 +04:00
If you want to configure Doctrine in more detail, take a look at the `Advanced
Configuration <reference/advanced-configuration>` section.
2010-11-01 23:16:12 +03:00
2010-11-02 00:03:50 +03:00
.. note ::
2013-03-16 23:36:09 +04:00
You can learn more about the database connection configuration in the
`Doctrine DBAL connection configuration reference <http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html> `_ .
2012-08-01 01:25:33 +04:00
2013-03-17 15:16:42 +04:00
Setting up the Commandline Tool
-------------------------------
Doctrine ships with a number of command line tools that are very helpful
during development. You can call this command from the Composer binary
directory:
.. code-block ::
$ php vendor/bin/doctrine
You need to register your applications EntityManager to the console tool
to make use of the tasks by creating a `` cli-config.php `` file with the
following content:
.. code-block :: php
<?php
// cli-config.php
require_once 'my_bootstrap.php';
// Any way to access the EntityManager from your application
$em = GetMyEntityManager();
$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)
));