File: index.rst

package info (click to toggle)
php-doctrine-persistence 4.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 656 kB
  • sloc: php: 3,669; makefile: 22
file content (332 lines) | stat: -rw-r--r-- 10,290 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
Introduction
============

The Doctrine Persistence project is a set of shared interfaces and functionality that the different Doctrine
object mappers share. You can use these interfaces and abstract classes to build your own mapper if you don't
want to use the full data mappers provided by Doctrine.

Installation
============

The library can easily be installed with composer.

.. code-block:: sh

    $ composer require doctrine/persistence

Overview
========

The interfaces and functionality in this project evolved from building several different implementations of Doctrine
object mappers. The first implementation was the ORM_ then came the `MongoDB ODM`_. A set of common interfaces were
extracted from both projects and released in the `Doctrine Common`_ project. Over the years, more common functionality
was extracted and eventually moved to this standalone project.
After that, that project was itself split into several projects
including this one.

A Doctrine object mapper looks like this when implemented.

.. code-block:: php

    final class User
    {
        /** @var string */
        private $username;

        public function __construct(string $username)
        {
            $this->username = $username;
        }

        // ...
    }

    $objectManager = new ObjectManager();
    $userRepository = $objectManager->getRepository(User::class);

    $newUser = new User('jwage');

    $objectManager->persist($newUser);
    $objectManager->flush();

    $user = $objectManager->find(User::class, 1);

    $objectManager->remove($user);
    $objectManager->flush();

    $users = $userRepository->findAll();

To learn more about the full interfaces and functionality continue reading!

Object Manager
==============

The main public interface that an end user will use is the ``Doctrine\Persistence\ObjectManager`` interface.

.. code-block:: php

    namespace Doctrine\Persistence;

    interface ObjectManager
    {
        public function find($className, $id);
        public function persist($object);
        public function remove($object);
        public function clear();
        public function detach($object);
        public function refresh($object);
        public function flush();
        public function getRepository($className);
        public function getClassMetadata($className);
        public function getMetadataFactory();
        public function initializeObject($obj);
        public function contains($object);
    }

ObjectRepository
================

The object repository is used to retrieve instances of your mapped objects from the mapper.

.. code-block:: php

    namespace Doctrine\Persistence;

    interface ObjectRepository
    {
        public function find($id);
        public function findAll();
        public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null);
        public function findOneBy(array $criteria);
        public function getClassName();
    }

Mapping
=======

In order for Doctrine to be able to persist your objects to a data store, you have to map the classes and class
properties so they can be properly stored and retrieved while maintaining a consistent state.

ClassMetadata
-------------

.. code-block:: php

    namespace Doctrine\Persistence\Mapping;

    interface ClassMetadata
    {
        public function getName();
        public function getIdentifier();
        public function getReflectionClass();
        public function isIdentifier($fieldName);
        public function hasField($fieldName);
        public function hasAssociation($fieldName);
        public function isSingleValuedAssociation($fieldName);
        public function isCollectionValuedAssociation($fieldName);
        public function getFieldNames();
        public function getIdentifierFieldNames();
        public function getAssociationNames();
        public function getTypeOfField($fieldName);
        public function getAssociationTargetClass($assocName);
        public function isAssociationInverseSide($assocName);
        public function getAssociationMappedByTargetField($assocName);
        public function getIdentifierValues($object);
    }

ClassMetadataFactory
--------------------

The ``Doctrine\Persistence\Mapping\ClassMetadataFactory`` class can be used to manage the instances for each of
your mapped PHP classes.

.. code-block:: php

    namespace Doctrine\Persistence\Mapping;

    interface ClassMetadataFactory
    {
        public function getAllMetadata();
        public function getMetadataFor($className);
        public function hasMetadataFor($className);
        public function setMetadataFor($className, $class);
        public function isTransient($className);
    }

Mapping Driver
==============

In order to load ``ClassMetadata`` instances you can use the ``Doctrine\Persistence\Mapping\Driver\MappingDriver``
interface. This is the interface that does the core loading of mapping information from wherever they are stored.
That may be in files, attributes, yaml, xml, etc.

.. code-block:: php

    namespace Doctrine\Persistence\Mapping\Driver;

    use Doctrine\Persistence\Mapping\ClassMetadata;

    interface MappingDriver
    {
        public function loadMetadataForClass($className, ClassMetadata $metadata);
        public function getAllClassNames();
        public function isTransient($className);
    }

The Doctrine Persistence project offers a few base implementations that
make it easy to implement your own XML, Attributes or YAML drivers.

FileDriver
----------

The file driver operates in a mode where it loads the mapping files of individual classes on demand. This requires
the user to adhere to the convention of 1 mapping file per class and the file names of the mapping files must
correspond to the full class name, including namespace, with the namespace delimiters '\', replaced by dots '.'.

Extend the ``Doctrine\Persistence\Mapping\Driver\FileDriver`` class to implement your own file driver.
Here is an example JSON file driver implementation.

.. code-block:: php

    use Doctrine\Persistence\Mapping\Driver\FileDriver;

    final class JSONFileDriver extends FileDriver
    {
        public function loadMetadataForClass($className, ClassMetadata $metadata)
        {
            $mappingFileData = $this->getElement($className);

            // use the array of mapping information from the file to populate the $metadata instance
        }

        protected function loadMappingFile($file)
        {
            return json_decode($file, true);
        }
    }

Now you can use it like the following.

.. code-block:: php

    use Doctrine\Persistence\Mapping\Driver\DefaultFileLocator;

    $fileLocator = new DefaultFileLocator('/path/to/mapping/files', 'json');

    $jsonFileDriver = new JSONFileDriver($fileLocator);

Now if you have a class named ``App\Model\User`` and you can load the mapping information like this.

.. code-block:: php

    use App\Model\User;
    use Doctrine\Persistence\Mapping\ClassMetadata;

    $classMetadata = new ClassMetadata();

    // looks for a file at /path/to/mapping/files/App.Model.User.json
    $jsonFileDriver->loadMetadataForClass(User::class, $classMetadata);


PHPDriver
---------

The PHPDriver includes PHP files which just populate ``ClassMetadata`` instances with plain PHP code.

.. code-block:: php

    use Doctrine\Persistence\Mapping\Driver\PHPDriver;

    $phpDriver = new PHPDriver('/path/to/mapping/files');

Now you can use it like the following:

.. code-block:: php

    use App\Model\User;
    use Doctrine\Persistence\Mapping\ClassMetadata;

    $classMetadata = new ClassMetadata();

    // looks for a PHP file at /path/to/mapping/files/App.Model.User.php
    $phpDriver->loadMetadataForClass(User::class, $classMetadata);

Inside the ``/path/to/mapping/files/App.Model.User.php`` file you can write raw PHP code to populate a ``ClassMetadata``
instance. You will have access to a variable named ``$metadata`` inside the file that you can use to populate the
mapping metadata.

.. code-block:: php

    use App\Model\User;

    $metadata->name = User::class;

    // ...

StaticPHPDriver
--------------

The StaticPHPDriver calls a static ``loadMetadata()`` method on your model classes where you can manually populate the
``ClassMetadata`` instance.

.. code-block:: php

    $staticPHPDriver = new StaticPHPDriver('/path/to/classes');

    $classMetadata = new ClassMetadata();

    // looks for a PHP file at /path/to/classes/App/Model/User.php
    $phpDriver->loadMetadataForClass(User::class, $classMetadata);

Your class in ``App\Model\User`` would look like the following.

.. code-block:: php

    namespace App\Model;

    final class User
    {
        // ...

        public static function loadMetadata(ClassMetadata $metadata)
        {
            // populate the $metadata instance
        }
    }

Reflection
==========

Doctrine uses reflection to set and get the data inside your objects. The
``Doctrine\Persistence\Mapping\ReflectionService`` is the primary interface needed for a Doctrine mapper.

.. code-block:: php

    namespace Doctrine\Persistence\Mapping;

    interface ReflectionService
    {
        public function getParentClasses($class);
        public function getClassShortName($class);
        public function getClassNamespace($class);
        public function getClass($class);
        public function getAccessibleProperty($class, $property);
        public function hasPublicMethod($class, $method);
    }

Doctrine provides an implementation of this interface in the class named
``Doctrine\Persistence\Mapping\RuntimeReflectionService``.

Implementations
===============

There are several different implementations of the Doctrine Persistence APIs.

- ORM_ - The Doctrine Object Relational Mapper is a data mapper for relational databases.
- `MongoDB ODM`_ - The Doctrine MongoDB ODM is a data mapper for MongoDB.
- `PHPCR ODM`_ - The Doctrine PHPCR ODM a data mapper built on top of the PHPCR API.

.. _ORM: https://www.doctrine-project.org/projects/orm.html
.. _MongoDB ODM: https://www.doctrine-project.org/projects/mongodb-odm.html
.. _PHPCR ODM: https://www.doctrine-project.org/projects/phpcr-odm.html
.. _Doctrine Common: https://www.doctrine-project.org/projects/common.html