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 10: Compon<strong>en</strong>t Overview 176<br />

Using Expression Values<br />

There might be situations where you need to use SQL expressions as values of columns. This<br />

can be achieved by using <strong>Doctrine</strong>_Expression which converts portable DQL expressions<br />

to your native SQL expressions.<br />

Lets say we have a class called ev<strong>en</strong>t with columns timepoint(datetime) and<br />

name(string). Saving the record with the curr<strong>en</strong>t timestamp can be achieved as follows:<br />

Listing<br />

10-60<br />

// test.php<br />

// ...<br />

$user = new User();<br />

$user->username = 'jwage';<br />

$user->updated_at = new <strong>Doctrine</strong>_Expression('NOW()');<br />

$user->save();<br />

The above code would issue the following SQL query:<br />

Listing<br />

10-61<br />

INSERT INTO user (username, updated_at_) VALUES ('jwage', NOW())<br />

Wh<strong>en</strong> you use <strong>Doctrine</strong>_Expression with your objects in order to get the updated value<br />

you will have to <strong>manual</strong>ly call refresh() to get the updated value from the database.<br />

Listing<br />

10-62<br />

// test.php<br />

// ...<br />

$user->refresh();<br />

Getting Record State<br />

Every <strong>Doctrine</strong>_Record has a state. First of all records can be transi<strong>en</strong>t or persist<strong>en</strong>t.<br />

Every record that is retrieved from database is persist<strong>en</strong>t and every newly created record is<br />

considered transi<strong>en</strong>t. If a <strong>Doctrine</strong>_Record is retrieved from database but the only loaded<br />

property is its primary key, th<strong>en</strong> this record has a state called proxy.<br />

Every transi<strong>en</strong>t and persist<strong>en</strong>t <strong>Doctrine</strong>_Record is either clean or dirty.<br />

<strong>Doctrine</strong>_Record is clean wh<strong>en</strong> none of its properties are changed and dirty wh<strong>en</strong> at least<br />

one of its properties has changed.<br />

A record can also have a state called locked. In order to avoid infinite recursion in some rare<br />

circular refer<strong>en</strong>ce cases <strong>Doctrine</strong> uses this state internally to indicate that a record is<br />

curr<strong>en</strong>tly under a manipulation operation.<br />

Below is a table containing all the differ<strong>en</strong>t states a record can be in with a short description<br />

of it:<br />

Name<br />

<strong>Doctrine</strong>_Record::STATE_PROXY<br />

Description<br />

Record is in proxy state meaning its persist<strong>en</strong>t but<br />

not all of its properties are loaded from the database.<br />

<strong>Doctrine</strong>_Record::STATE_TCLEAN Record is transi<strong>en</strong>t clean, meaning its transi<strong>en</strong>t and<br />

none of its properties are changed.<br />

<strong>Doctrine</strong>_Record::STATE_TDIRTY Record is transi<strong>en</strong>t dirty, meaning its transi<strong>en</strong>t and<br />

some of its properties are changed.<br />

<strong>Doctrine</strong>_Record::STATE_DIRTY<br />

Record is dirty, meaning its persist<strong>en</strong>t and some of<br />

its properties are changed.<br />

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

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

Saved successfully!

Ooh no, something went wrong!