|
1 | 1 | PHP Mapping |
2 | 2 | =========== |
3 | 3 |
|
4 | | -Doctrine ORM also allows you to provide the ORM metadata in the form |
5 | | -of plain PHP code using the ``ClassMetadata`` API. You can write |
6 | | -the code in PHP files or inside of a static function named |
7 | | -``loadMetadata($class)`` on the entity class itself. |
8 | | - |
9 | | -PHP Files |
10 | | ---------- |
11 | | - |
12 | | -If you wish to write your mapping information inside PHP files that |
13 | | -are named after the entity and included to populate the metadata |
14 | | -for an entity you can do so by using the ``PHPDriver``: |
15 | | - |
16 | | -.. code-block:: php |
17 | | -
|
18 | | - <?php |
19 | | - $driver = new PHPDriver('/path/to/php/mapping/files'); |
20 | | - $em->getConfiguration()->setMetadataDriverImpl($driver); |
21 | | -
|
22 | | -Now imagine we had an entity named ``Entities\User`` and we wanted |
23 | | -to write a mapping file for it using the above configured |
24 | | -``PHPDriver`` instance: |
25 | | - |
26 | | -.. code-block:: php |
27 | | -
|
28 | | - <?php |
29 | | - namespace Entities; |
30 | | -
|
31 | | - class User |
32 | | - { |
33 | | - private $id; |
34 | | - private $username; |
35 | | - } |
36 | | -
|
37 | | -To write the mapping information you just need to create a file |
38 | | -named ``Entities.User.php`` inside of the |
39 | | -``/path/to/php/mapping/files`` folder: |
40 | | - |
41 | | -.. code-block:: php |
42 | | -
|
43 | | - <?php |
44 | | - // /path/to/php/mapping/files/Entities.User.php |
45 | | -
|
46 | | - $metadata->mapField(array( |
47 | | - 'id' => true, |
48 | | - 'fieldName' => 'id', |
49 | | - 'type' => 'integer' |
50 | | - )); |
51 | | -
|
52 | | - $metadata->mapField(array( |
53 | | - 'fieldName' => 'username', |
54 | | - 'type' => 'string', |
55 | | - 'options' => array( |
56 | | - 'fixed' => true, |
57 | | - 'comment' => "User's login name" |
58 | | - ) |
59 | | - )); |
60 | | -
|
61 | | - $metadata->mapField(array( |
62 | | - 'fieldName' => 'login_count', |
63 | | - 'type' => 'integer', |
64 | | - 'nullable' => false, |
65 | | - 'options' => array( |
66 | | - 'unsigned' => true, |
67 | | - 'default' => 0 |
68 | | - ) |
69 | | - )); |
70 | | -
|
71 | | -Now we can easily retrieve the populated ``ClassMetadata`` instance |
72 | | -where the ``PHPDriver`` includes the file and the |
73 | | -``ClassMetadataFactory`` caches it for later retrieval: |
74 | | - |
75 | | -.. code-block:: php |
76 | | -
|
77 | | - <?php |
78 | | - $class = $em->getClassMetadata('Entities\User'); |
79 | | - // or |
80 | | - $class = $em->getMetadataFactory()->getMetadataFor('Entities\User'); |
| 4 | +Doctrine ORM also allows you to provide the ORM metadata in the form of plain |
| 5 | +PHP code using the ``ClassMetadata`` API. You can write the code in inside of a |
| 6 | +static function named ``loadMetadata($class)`` on the entity class itself. |
81 | 7 |
|
82 | 8 | Static Function |
83 | 9 | --------------- |
84 | 10 |
|
85 | | -In addition to the PHP files you can also specify your mapping |
86 | | -information inside of a static function defined on the entity class |
87 | | -itself. This is useful for cases where you want to keep your entity |
88 | | -and mapping information together but don't want to use annotations. |
89 | | -For this you just need to use the ``StaticPHPDriver``: |
| 11 | +In addition to other drivers using configuration languages you can also |
| 12 | +programatically specify your mapping information inside of a static function |
| 13 | +defined on the entity class itself. |
| 14 | + |
| 15 | +This is useful for cases where you want to keep your entity and mapping |
| 16 | +information together but don't want to use annotations. For this you just need |
| 17 | +to use the ``StaticPHPDriver``: |
90 | 18 |
|
91 | 19 | .. code-block:: php |
92 | 20 |
|
|
0 commit comments