Installing Doctrine
The following steps should be performed to install Doctrine:
To install Doctrine, we need to create a file called
composer.json
in our newblog
directory. It lists dependencies of our project as shown in the following code:{ "name": "myname/blog", "type": "project", "description": "My small blog to play with Doctrine", "require": { "doctrine/orm": "2.4.*" }, "autoload": { "psr-0": { "": "src/" } } }
This standard JSON file will be parsed by Composer to download and install all dependencies specified. Once installed, Composer will load all classes of these libraries automatically.
The
name
,type
, anddescription
attributes are optional but it's a good practice to always fill them. They provide general information about the project we are working on.The more interesting part of this
composer.json
file is therequire
field. In order to get it installed by Composer, all libraries used by our app must be listed here. A lot of PHP libraries are available on Packagist, the default Composer package repository. Of course, it's the case of Doctrine projects.Note
For more information on Packagist, go through the following link: https://packagist.org/
We indicate that we need the latest minor release of the 2.4 branch of Doctrine ORM. You can set a major or minor version here, and even more complicated things.
Note
For more information on a package version, you can refer to the following link: http://getcomposer.org/doc/01-basic-usage.md#package-versions
The
autoload
field is here to tell Composer to automatically load classes of our app. We will put our specific code in a directory calledsrc/
. Our files and classes will follow thePSR-0
namespacing and file-naming standard.Note
PHP Specification Requests are attempts to improve interoperability of PHP applications and libraries. They are available at http://www.php-fig.org/.
It's time to use Composer to install the ORM. Run the following command:
php composer.phar install
New files appear in the
vendor/
directory. Doctrine ORM has been installed, and Composer was smart enough to get all its dependencies, including Doctrine DBAL and Doctrine Common.A
composer.lock
file has also been created. It contains exact versions of installed libraries. This is useful for deploying applications. Thanks to this file, when running theinstall
command, Composer will be able to retrieve the same versions that have been used in the development.Doctrine is now properly installed. Easy, isn't it?
To update libraries when there are new releases in the 2.4 branch, we just need to type the following command:
php composer.phar update