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 16: Behaviors 248<br />

Listing<br />

16-25<br />

---<br />

BlogPost:<br />

actAs:<br />

Versionable:<br />

versionColumn: version<br />

className: %CLASS%Version<br />

auditLog: true<br />

columns:<br />

title: string(255)<br />

body: clob<br />

The auditLog option can be used to turn off the audit log history. This is wh<strong>en</strong> you want<br />

to maintain a version number but not maintain the data at each version.<br />

Lets check the SQL that is g<strong>en</strong>erated by the above models:<br />

Listing<br />

16-26<br />

// test.php<br />

Listing<br />

16-27<br />

// ...<br />

$sql = <strong>Doctrine</strong>_Core::g<strong>en</strong>erateSqlFromArray(array('BlogPost'));<br />

echo $sql[0] . "\n";<br />

echo $sql[1];<br />

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

CREATE TABLE blog_post_version (id BIGINT,<br />

title VARCHAR(255),<br />

body LONGTEXT,<br />

version BIGINT,<br />

PRIMARY KEY(id,<br />

version)) ENGINE = INNODB<br />

CREATE TABLE blog_post (id BIGINT AUTO_INCREMENT,<br />

title VARCHAR(255),<br />

body LONGTEXT,<br />

version BIGINT,<br />

PRIMARY KEY(id)) ENGINE = INNODB<br />

ALTER TABLE blog_post_version ADD FOREIGN KEY (id) REFERENCES<br />

blog_post(id) ON UPDATE CASCADE ON DELETE CASCADE<br />

Notice how we have 2 additional statem<strong>en</strong>ts we probably didn't expect to see. The behavior<br />

automatically created a blog_post_version table and related it to blog_post.<br />

Listing<br />

16-28<br />

Now wh<strong>en</strong> we insert or update a BlogPost the version table will store all the old versions of<br />

the record and allow you to revert back at anytime. Wh<strong>en</strong> you instantiate a BlogPost for the<br />

first time this is what is happ<strong>en</strong>ing internally:<br />

• It creates a class called BlogPostVersion on-the-fly, the table this record is<br />

pointing at is blog_post_version<br />

• Everytime a BlogPost object is deleted / updated the previous version is stored into<br />

blog_post_version<br />

• Everytime a BlogPost object is updated its version number is increased.<br />

Now lets play around with the BlogPost model:<br />

$blogPost = new BlogPost();<br />

$blogPost->title = 'Test blog post';<br />

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

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

Saved successfully!

Ooh no, something went wrong!