background preloader

Doctrine2 ORM

Facebook Twitter

La couche métier : les entités. Doctrine et les bases de données (master. L'une des tâches les plus courantes et difficiles pour toute application consiste à lire et à persister des informations dans une base de données.

Doctrine et les bases de données (master

Heureusement, Symfony intègre Doctrine, une bibliothèque dont le seul but est de vous fournir des outils puissants afin de vous faciliter la tâche. Dans ce chapitre, vous apprendrez les bases de la philosophie de Doctrine et verrez à quel point il peut être facile de travailler avec une base de données. Note Doctrine est totalement découplé de Symfony et son utilisation est optionnelle. Ce chapitre est entièrement consacré à l'ORM Doctrine, dont l'objectif est de mapper vos objets avec une base de données relationnelle (comme MySQL, PostGresSQL ou Microsoft SQL).

5. Basic Mapping. This guide explains the basic mapping of entities and properties.

5. Basic Mapping

After working through this guide you should know: How to create PHP objects that can be saved to the database with Doctrine;How to configure the mapping between columns on tables and properties on entities;What Doctrine mapping types are;Defining primary keys and how identifiers are generated by Doctrine;How quoting of reserved symbols works in Doctrine.

Mapping of associations will be covered in the next chapter on Association Mapping. 5.2. Creating Classes for the Database Every PHP object that you want to save in the database using Doctrine is called an “Entity”. <? Because Doctrine is a generic library, it only knows about your entities because you will describe their existence and structure using mapping metadata, which is configuration that tells Doctrine how your entity should be stored in the database. Doctrine provides several different ways to specify object-relational mapping metadata: Note PHP<? Manipuler ses entités avec Doctrine2. Class Doctrine\ORM\EntityManager. The EntityManager is the central access point to ORM functionality. implements Doctrine\Common\Persistence\ObjectManager protected # __construct ( Doctrine\DBAL\Connection , Doctrine\ORM\Configuration , Doctrine\Common\EventManager ) Creates a new EntityManager that operates on the given database connection and uses the given Configuration and EventManager implementations.

Parameters. DoctrineFixturesBundle. Welcome to Doctrine 2 ORM’s documentation! Les relations entre entités avec Doctrine2. 6. Association Mapping. This chapter explains mapping associations between objects.

6. Association Mapping

Instead of working with foreign keys in your code, you will always work with references to objects instead and Doctrine will convert those references to foreign keys internally. 9. Working with Associations. Associations between entities are represented just like in regular object-oriented PHP code using references to other objects or collections of objects.

9. Working with Associations

Changes to associations in your code are not synchronized to the database directly, only when calling EntityManager#flush(). There are other concepts you should know about when working with associations in Doctrine: If an entity is removed from a collection, the association is removed, not the entity itself. A collection of entities always only represents the association to the containing entities, not the entity itself.When a bidirectional assocation is updated, Doctrine only checks on one of both sides for these changes.

This is called the owning side of the association.A property with a reference to many entities has to be instances of the Doctrine\Common\Collections\Collection interface. 9.1. We will use a simple comment system with Users and Comments as entities to show examples of association management. <? Récupérer ses entités avec Doctrine2. 15. Doctrine Query Language. 15.2.1.

15. Doctrine Query Language

DQL SELECT clause The select clause of a DQL query specifies what appears in the query result. The composition of all the expressions in the select clause also influences the nature of the query result. Here is an example that selects all users with an age > 20: <? Lets examine the query: u is a so called identification variable or alias that refers to the MyProject\Model\User class. The result of this query would be a list of User objects where all users are older than 20. The SELECT clause allows to specify both class identification variables that signal the hydration of a complete entity class or just fields of the entity using the syntax u.name.

16. The QueryBuilder. A QueryBuilder provides an API that is designed for conditionally constructing a DQL query in several steps.

16. The QueryBuilder

It provides a set of classes and methods that is able to programmatically build queries, and also provides a fluent API. This means that you can change between one methodology to the other as you want, and also pick one if you prefer. 16.1. Constructing a new QueryBuilder object The same way you build a normal Query, you build a QueryBuilder object, just providing the correct method name. <? Once you have created an instance of QueryBuilder, it provides a set of useful informative functions that you can use. <? There’re currently 3 possible return values for getType(): QueryBuilder::SELECT, which returns value 0QueryBuilder::DELETE, returning value 1QueryBuilder::UPDATE, which returns value 2 It is possible to retrieve the associated EntityManager of the current QueryBuilder, its DQL and also a Query object when you finish building your DQL.

Les évènements Doctrine. L'intérêt des évènements Doctrine Dans certains cas, vous pouvez avoir besoin d'effectuer des actions juste avant ou juste après la création, la mise à jour ou la suppression d'une entité.

Les évènements Doctrine

Par exemple, si vous stockez la date d'édition d'un article, à chaque modification de l'entité Article il faut mettre à jour cet attribut juste avant la mise à jour dans la base de données. 10. Events.