10.04.2018 Views

Doctrine_manual-1-2-en

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 7: Defining Models 56<br />

Type modifiers<br />

Within the <strong>Doctrine</strong> API there are a few modifiers that have be<strong>en</strong> designed to aid in optimal<br />

table design. These are:<br />

• The notnull modifiers<br />

• The l<strong>en</strong>gth modifiers<br />

• The default modifiers<br />

• unsigned modifiers for some field definitions, although not all DBMS's support this<br />

modifier for integer field types.<br />

• collation modifiers (not supported by all drivers)<br />

• fixed l<strong>en</strong>gth modifiers for some field definitions.<br />

Building upon the above, we can say that the modifiers alter the field definition to create<br />

more specific field types for specific usage sc<strong>en</strong>arios. The notnull modifier will be used in the<br />

following way to set the default DBMS NOT NULL Flag on the field to true or false,<br />

dep<strong>en</strong>ding on the DBMS's definition of the field value: In PostgreSQL the "NOT NULL"<br />

definition will be set to "NOT NULL", whilst in MySQL (for example) the "NULL" option will<br />

be set to "NO". In order to define a "NOT NULL" field type, we simply add an extra parameter<br />

to our definition array (See the examples in the following section)<br />

Listing<br />

7-7<br />

'sometime' = array(<br />

'type' => 'time',<br />

'default' => '12:34:05',<br />

'notnull' => true,<br />

),<br />

Using the above example, we can also explore the default field operator. Default is set in the<br />

same way as the notnull operator to set a default value for the field. This value may be set in<br />

any character set that the DBMS supports for text fields, and any other valid data for the<br />

field's data type. In the above example, we have specified a valid time for the "Time" data<br />

type, '12:34:05'. Remember that wh<strong>en</strong> setting default dates and times, as well as datetimes,<br />

you should research and stay within the epoch of your chos<strong>en</strong> DBMS, otherwise you will<br />

<strong>en</strong>counter difficult to diagnose errors!<br />

Listing<br />

7-8<br />

'sometext' = array(<br />

'type' => 'string',<br />

'l<strong>en</strong>gth' => 12,<br />

),<br />

The above example will create a character varying field of l<strong>en</strong>gth 12 characters in the<br />

database table. If the l<strong>en</strong>gth definition is left out, <strong>Doctrine</strong> will create a l<strong>en</strong>gth of the<br />

maximum allowable l<strong>en</strong>gth for the data type specified, which may create a problem with some<br />

field types and indexing. Best practice is to define l<strong>en</strong>gths for all or most of your fields.<br />

Boolean<br />

The boolean data type repres<strong>en</strong>ts only two values that can be either 1 or 0. Do not assume<br />

that these data types are stored as integers because some DBMS drivers may implem<strong>en</strong>t this<br />

type with single character text fields for a matter of effici<strong>en</strong>cy. Ternary logic is possible by<br />

using null as the third possible value that may be assigned to fields of this type.<br />

The next several examples are not meant for you to use and give them a try. They are<br />

simply for demonstrating purposes to show you how to use the differ<strong>en</strong>t <strong>Doctrine</strong> data<br />

types using PHP code or YAML schema files.<br />

----------------- Brought to you by

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!