Doctrine. Structure. Entity

Right after starting to work with Doctrine I acquired some prejudices. I have since eradicated them, but maybe someone still has them, maybe this note will help someone =)

In addition to what was written in this note (that Doctrine is an abstract layer for working with databases), Doctrine itself consists of several layers for working with databases:


Both layers use Common

ORM(ODM) includes DBAL.

These three packages are part of Doctrine.

Let's start with Common. This component does not have a direct relation to the database (that's why I didn't call it the third layer of Doctrine), but nevertheless it is used by other Doctrine components.

It consists of three parts:

  1. Class Loading
  2. Doctrine Annotations
  3. Doctrine Caching
Class Loading is a php class autoloader (it can perform the same functions as the composer autoloader)

Doctrine Annotations is a powerful tool for... how can I put it... interpreting php doc blocks into program code.

Annotations were originally used for ORM/ODM mapping, but they are versatile enough. By using the component, you can create your own annotations. In Symfony framework, the most common use of annotations is for ORM/ODM mapping and Validation Constraints (allows you to specify validation parameters).

Doctrine Caching - a component for temporary storage (caching) of data. It is quite easy to use and convenient. DBAL is a basic extension over PDO.

Among the interesting features, it provides OOP facilities for writing queries.

And finally, we smoothly transitioned to  ORM(ODM) and prejudices =).

ORM (Object-relational mapping) represents data from a relational database in an object-oriented way.

ODM - the same for document databases (like mongodb).

It eliminates the need to write direct queries (although they are still necessary for complex manipulations). Data access is performed through object manipulations.

My main mistake at the beginning was that I did not pay enough attention to the description of ORM - entities(entity), simply creating the structure in phpmyadmin and importing it (how to do it is described here)

Entity - is a kind of object "image" of an entity stored in the database. 

In the simplest case, an entity is mapped to a table, but this is not always the case. For example, in the case of describing complex relationships (many-to-many, for example), an additional intermediate table for relationships will be created.

A simple example of an Entity (from Symfony's documentation):

namespace AcmeStoreBundleEntity;

use DoctrineORMMapping as ORM;

/** * @ORMEntity * @ORMTable(name="product") */ class Product { /** * @ORMColumn(type="integer") * @ORMId * @ORMGeneratedValue(strategy="AUTO") */ protected $id;

/** * @ORMColumn(type="string", length=100) */ protected $name;

/** * @ORMColumn(type="decimal", scale=2) */ protected $price;

/** * @ORMColumn(type="text") */ protected $description; }

As we can see, an Entity is simply a storage class with the help of annotations that specify the properties of the database (type, relationships, column name in the database, allowed values, etc.).