configuration.rst 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. Installation and Configuration
  2. ==============================
  3. Doctrine can be installed with `Composer <http://www.getcomposer.org>`_. For
  4. older versions we still have `PEAR packages
  5. <http://pear.doctrine-project.org>`_.
  6. Define the following requirement in your ``composer.json`` file:
  7. ::
  8. {
  9. "require": {
  10. "doctrine/orm": "*"
  11. }
  12. }
  13. Then call ``composer install`` from your command line. If you don't know
  14. how Composer works, check out their `Getting Started
  15. <http://getcomposer.org/doc/00-intro.md>`_ to set up.
  16. Class loading
  17. -------------
  18. Autoloading is taken care of by Composer. You just have to include the composer autoload file in your project:
  19. .. code-block:: php
  20. <?php
  21. // bootstrap.php
  22. // Include Composer Autoload (relative to project root).
  23. require_once "vendor/autoload.php";
  24. Obtaining an EntityManager
  25. --------------------------
  26. Once you have prepared the class loading, you acquire an
  27. *EntityManager* instance. The EntityManager class is the primary
  28. access point to ORM functionality provided by Doctrine.
  29. .. code-block:: php
  30. <?php
  31. // bootstrap.php
  32. require_once "vendor/autoload.php";
  33. use Doctrine\ORM\Tools\Setup;
  34. use Doctrine\ORM\EntityManager;
  35. $paths = array("/path/to/entities-or-mapping-files");
  36. $isDevMode = false;
  37. // the connection configuration
  38. $dbParams = array(
  39. 'driver' => 'pdo_mysql',
  40. 'user' => 'root',
  41. 'password' => '',
  42. 'dbname' => 'foo',
  43. );
  44. $config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
  45. $entityManager = EntityManager::create($dbParams, $config);
  46. Or if you prefer XML:
  47. .. code-block:: php
  48. <?php
  49. $config = Setup::createXMLMetadataConfiguration($paths, $isDevMode);
  50. $entityManager = EntityManager::create($dbParams, $config);
  51. Or if you prefer YAML:
  52. .. code-block:: php
  53. <?php
  54. $config = Setup::createYAMLMetadataConfiguration($paths, $isDevMode);
  55. $entityManager = EntityManager::create($dbParams, $config);
  56. Inside the ``Setup`` methods several assumptions are made:
  57. - If `$devMode` is true always use an ``ArrayCache`` (in-memory) and regenerate proxies on every request.
  58. - 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.
  59. - If `$devMode` is false, set then proxy classes have to be explicitly created
  60. through the command line.
  61. - If third argument `$proxyDir` is not set, use the systems temporary directory.
  62. If you want to configure Doctrine in more detail, take a look at the :doc:`Advanced
  63. Configuration <reference/advanced-configuration>` section.
  64. .. note::
  65. You can learn more about the database connection configuration in the
  66. `Doctrine DBAL connection configuration reference <http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html>`_.
  67. Setting up the Commandline Tool
  68. -------------------------------
  69. Doctrine ships with a number of command line tools that are very helpful
  70. during development. You can call this command from the Composer binary
  71. directory:
  72. .. code-block:: sh
  73. $ php vendor/bin/doctrine
  74. You need to register your applications EntityManager to the console tool
  75. to make use of the tasks by creating a ``cli-config.php`` file with the
  76. following content:
  77. On Doctrine 2.4 and above:
  78. .. code-block:: php
  79. <?php
  80. use Doctrine\ORM\Tools\Console\ConsoleRunner;
  81. // replace with file to your own project bootstrap
  82. require_once 'bootstrap.php';
  83. // replace with mechanism to retrieve EntityManager in your app
  84. $entityManager = GetEntityManager();
  85. return ConsoleRunner::createHelperSet($entityManager);
  86. On Doctrine 2.3 and below:
  87. .. code-block:: php
  88. <?php
  89. // cli-config.php
  90. require_once 'my_bootstrap.php';
  91. // Any way to access the EntityManager from your application
  92. $em = GetMyEntityManager();
  93. $helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
  94. 'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
  95. 'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
  96. ));