You are browsing a version that is no longer maintained. |
Installation and Configuration
Doctrine can be installed with Composer.
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 to set up.
Class loading
Autoloading is taken care of by Composer. You just have to include the composer autoload file in your project:
<?php// bootstrap.php// Include Composer Autoload (relative to project root).require_once "vendor/autoload.php";
Obtaining an EntityManager
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.
<?php// bootstrap.phprequire_once "vendor/autoload.php";use Doctrine\ORM\Tools\Setup;use Doctrine\ORM\EntityManager;$paths = array("/path/to/entity-files");$isDevMode = false;// the connection configuration$dbParams = array( 'driver' => 'pdo_mysql', 'user' => 'root', 'password' => '', 'dbname' => 'foo',);$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);$entityManager = EntityManager::create($dbParams, $config);
Or if you prefer XML:
<?php$paths = array("/path/to/xml-mappings");$config = Setup::createXMLMetadataConfiguration($paths, $isDevMode);$entityManager = EntityManager::create($dbParams, $config);
Or if you prefer YAML:
<?php$paths = array("/path/to/yml-mappings");$config = Setup::createYAMLMetadataConfiguration($paths, $isDevMode);$entityManager = EntityManager::create($dbParams, $config);
|
If you want to use yml mapping you should add yaml dependency to your
|
Inside the Setup methods several assumptions are made:
- If
$isDevModeis true caching is done in memory with theArrayCache. Proxy objects are recreated on every request. - If
$isDevModeis false, check for Caches in the order APC, Xcache, Memcache (127.0.0.1:11211), Redis (127.0.0.1:6379) unless$cacheis passed as fourth argument. - If
$isDevModeis false, set then proxy classes have to be explicitly created through the command line. - If third argument
$proxyDiris not set, use the systems temporary directory.
If you want to configure Doctrine in more detail, take a look at the Advanced Configuration section.
|
You can learn more about the database connection configuration in the Doctrine DBAL connection configuration reference. |
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:
$ 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:
<?phpuse Doctrine\ORM\Tools\Console\ConsoleRunner;// replace with file to your own project bootstraprequire_once 'bootstrap.php';// replace with mechanism to retrieve EntityManager in your app$entityManager = GetEntityManager();return ConsoleRunner::createHelperSet($entityManager);
