You are browsing a version that is no longer maintained.

PHP Mapping

Doctrine ORM also allows you to provide the ORM metadata in the form of plain PHP code using the ClassMetadata API. You can write the code in PHP files or inside of a static function named loadMetadata($class) on the entity class itself.

PHP Files

PHPDriver is deprecated and will be removed in 3.0, use StaticPHPDriver instead.

If you wish to write your mapping information inside PHP files that are named after the entity and included to populate the metadata for an entity you can do so by using the PHPDriver:

1<?php $driver = new PHPDriver('/path/to/php/mapping/files'); $em->getConfiguration()->setMetadataDriverImpl($driver);
2
3

Now imagine we had an entity named Entities\User and we wanted to write a mapping file for it using the above configured PHPDriver instance:

1<?php namespace Entities; class User { private $id; private $username; }
2
3
4
5
6
7
8

To write the mapping information you just need to create a file named Entities.User.php inside of the /path/to/php/mapping/files folder:

1<?php // /path/to/php/mapping/files/Entities.User.php $metadata->mapField(array( 'id' => true, 'fieldName' => 'id', 'type' => 'integer' )); $metadata->mapField(array( 'fieldName' => 'username', 'type' => 'string', 'options' => array( 'fixed' => true, 'comment' => "User's login name" ) )); $metadata->mapField(array( 'fieldName' => 'login_count', 'type' => 'integer', 'nullable' => false, 'options' => array( 'unsigned' => true, 'default' => 0 ) ));
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

Now we can easily retrieve the populated ClassMetadata instance where the PHPDriver includes the file and the ClassMetadataFactory caches it for later retrieval:

1<?php $class = $em->getClassMetadata('Entities\User'); // or $class = $em->getMetadataFactory()->getMetadataFor('Entities\User');
2
3
4

Static Function

In addition to the PHP files you can also specify your mapping information inside of a static function defined on the entity class itself. This is useful for cases where you want to keep your entity and mapping information together but don't want to use attributes or annotations. For this you just need to use the StaticPHPDriver:

1<?php use Doctrine\Persistence\Mapping\Driver\StaticPHPDriver; $driver = new StaticPHPDriver('/path/to/entities'); $em->getConfiguration()->setMetadataDriverImpl($driver);
2
3
4
5

Now you just need to define a static function named loadMetadata($metadata) on your entity:

1<?php namespace Entities; use Doctrine\ORM\Mapping\ClassMetadata; class User { // ... public static function loadMetadata(ClassMetadata $metadata) { $metadata->mapField(array( 'id' => true, 'fieldName' => 'id', 'type' => 'integer' )); $metadata->mapField(array( 'fieldName' => 'username', 'type' => 'string' )); } }
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23