Mapping in Doctrine 2 is necessary to tell Doctrine 2 which classes are to be saved in the database and which fields in those class will be used for this purpose.
This post continues the earlier post which shows how to create Doctrine 2 classes which can be found in our post Doctrine 2 Classes – how to create them and all you need to know.
One major benefit to mapping in Doctrine 2 that often gets overlooked is that because mapping is stored in files and is used by Doctrine 2 for building the database schema, this means that the database structure can be versioned.
There are three different types of mapping in Doctrine 2. These are:
- Annotations – PHP comments
- XML – uses XML files
- YAML – uses YAML – like XML, but with the YAML format
My personal preferred method of mapping in Doctrine 2 is XML, I just find it much cleaner and easier to understand than the alternatives, therefore this post will demonstrate Doctrine 2 mapping using XML files.
To set up mapping with XML files, you first need to create a mappings folder on the same level as the Doctrine 2 models and set this path in the bootstrap. It is also worth noting that all Doctrine 2 XML files must end with the “.dcm.xml” extension.
You then create your mappings file in relation to your Doctrine 2 model file. If the model is called Default_Model_User then the mappings file will be called Default_Model_User.dcm.xml
The mapping file consists of 4 elements:
- doctrine-mapping element – defines the doctrine-mapping element and the schema location
- entity element – refers to the class name and the database table
- ID element – identifies the table ID
- field element – used to identify table fields. Only standard elements and not a foreign relationship or primary key
Within the field element there are multiple types of field such as string, boolean, date, integer etc. Below is a list of all field types
- string: Type that maps an SQL VARCHAR to a PHP string
- integer: Type that maps an SQL INT to a PHP integer
- smallint: Type that maps a database SMALLINT to a PHP integer
- bigint: Type that maps a database BIGINT to a PHP string
- boolean: Type that maps an SQL boolean to a PHP boolean
- decimal: Type that maps an SQL DECIMAL to a PHP double
- date: Type that maps an SQL DATETIME to a PHP DateTime object
- time: Type that maps an SQL TIME to a PHP DateTime object
- datetime: Type that maps an SQL DATETIME/TIMESTAMP to a PHP DateTime object
- text: Type that maps an SQL CLOB to a PHP string.
- object: Type that maps a SQL CLOB to a PHP object using serialize() and unserialize()
- array: Type that maps a SQL CLOB to a PHP object using serialize() and unserialize()
- float: Type that maps a SQL Float (Double Precision) to a PHP double. IMPORTANT: Works only with locale settings that use decimal points as separator
An example Doctrine 2 mapping file could look like:
<?xml version="1.0" encoding="UTF-8"?> <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> <entity name="User" table="users"> <id name="id" type="integer" column="id"> <generator strategy="AUTO" /> </id> <field name="username" type="string" nullable="false"/> <field name="password" type="text" nullable="false" /> </entity> </doctrine-mapping>