Preface
Doctrine 2 has become the most popular modern persistence system for PHP. It is distributed with the standard edition of the Symfony2 framework, can be used standalone in any PHP project and integrates very well with Zend Framework 2, CodeIgniter, or Laravel. It is efficient, automatically abstracts popular database managing systems, supports PHP 5.3 features (including namespaces), is installable through Composer, and has an extensively tested quality code base.
Doctrine's ORM library allows easy persisting and retrieving of PHP object graph, without writing any SQL query by hand. It also provides a powerful object-oriented SQL-like query language called DQL, a database schema generator tool, an event system, and much more.
To discover this must-have library, we will together build a typical small, blog engine.
What this book covers
Chapter 1, Getting Started with Doctrine 2, explains how to install Common, DBAL, and ORM libraries through Composer, get our first entity manager, and configure command-line tools after presenting the project we built throughout the book (the architecture of Doctrine and the configuration of the development environment).
Chapter 2, Entities and Mapping Information, introduces the concept of Doctrine entities. We will create a first entity, map it to the database with annotations, generate the database schema, create data fixtures, and, finally, lay the foundation of the user interface of the blog.
Chapter 3, Associations, explains how to handle associations between the PHP objects and the ORM. We will create new entities, detail one-to-one, one-to-many, and many-to-many associations, generate the underlying database schema, create data fixtures and use associations in the user interface.
Chapter 4, Building Queries, creates entity repositories and helps understand how to use the query builder for generating DQL queries and retrieving entities. We will also take a look at aggregate functions.
Chapter 5, Going Further, will take a look at the advanced features of Doctrine. We will see different ways in which Doctrine can manage object inheritance, play with entity lifecycle events, and create native SQL queries.
What you need for this book
To execute examples of this book, you just need PHP 5.4+ a text editor, or a PHP IDE, and your favorite browser.
Who this book is for
Readers should have a good knowledge of object-oriented programming, PHP (including features introduced in PHP 5.3 and 5.4), and general database concepts.
Conventions
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, and user input are shown as follows: "The NativeQuery
class allows you to execute native SQL queries and to get their results as Doctrine entities."
A block of code is set as follows:
/** * Adds comment * * @param Comment $comment * @return Post */ public function addComment(Comment $comment) { $this->comments[] = $comment; $comment->setPost($this); return $this; }
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
/**
* Adds comment
*
* @param Comment $comment
* @return Post
*/
public function addComment(Comment $comment)
{
$this->comments[] = $comment;
$comment->setPost($this);
return $this;
}
Any command-line input or output is written as follows:
# php bin/load-fixtures.php
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "The following text must be printed in the terminal: ATTENTION: This operation should not be executed in a production environment."
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <feedback@packtpub.com>
, and mention the book title via the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <copyright@packtpub.com>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
Questions
You can contact us at <questions@packtpub.com>
if you are having a problem with any aspect of the book, and we will do our best to address it.