10.04.2018 Views

Doctrine_manual-1-2-en

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 13: Data Validation 211<br />

# ...<br />

minl<strong>en</strong>gth: 12<br />

Examples<br />

Not Null<br />

A not-null constraint simply specifies that a column must not assume the null value. A not-null<br />

constraint is always writt<strong>en</strong> as a column constraint.<br />

The following definition uses a notnull constraint for column name. This means that the<br />

specified column doesn't accept null values.<br />

// models/User.php<br />

Listing<br />

13-3<br />

class User ext<strong>en</strong>ds BaseUser<br />

{<br />

// ...<br />

public function setTableDefinition()<br />

{<br />

par<strong>en</strong>t::setTableDefinition();<br />

// ...<br />

}<br />

}<br />

$this->hasColumn('username', 'string', 255, array(<br />

'notnull' => true,<br />

'primary' => true,<br />

)<br />

);<br />

Here is the same example in YAML format. You can read more about YAML in the YAML<br />

Schema Files (page 195) chapter:<br />

---<br />

# schema.yml<br />

Listing<br />

13-4<br />

# ...<br />

User:<br />

columns:<br />

username:<br />

type: string(255)<br />

notnull: true<br />

primary: true<br />

# ...<br />

Wh<strong>en</strong> this class gets exported to database the following SQL statem<strong>en</strong>t would get executed<br />

(in MySQL):<br />

CREATE TABLE user (username VARCHAR(255) NOT NULL,<br />

PRIMARY KEY(username))<br />

Listing<br />

13-5<br />

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

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

Saved successfully!

Ooh no, something went wrong!