<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>laneolson.ca &#187; Best Practices</title>
	<atom:link href="http://www.laneolson.ca/category/best-practices/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.laneolson.ca</link>
	<description></description>
	<lastBuildDate>Tue, 08 Jun 2010 20:25:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Critical Files on Dropbox: A Cautionary Tale</title>
		<link>http://www.laneolson.ca/2010/02/08/critical-files-on-dropbox-a-cautionary-tale/</link>
		<comments>http://www.laneolson.ca/2010/02/08/critical-files-on-dropbox-a-cautionary-tale/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 16:03:27 +0000</pubDate>
		<dc:creator>Lane</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[Backup]]></category>
		<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Dropbox]]></category>
		<category><![CDATA[KeePass]]></category>

		<guid isPermaLink="false">http://www.laneolson.ca/?p=144</guid>
		<description><![CDATA[<p>This weekend I had somewhat of an unsettling experience with <a href="https://www.dropbox.com/" target="_blank">Dropbox</a>.  Typically I keep my <a href="http://keepass.info/" target="_blank">KeePass database</a> stored within Dropbox so that it is automatically synchronized between all of my computers.  I've felt quite confident that, because the file is synchronized to multiple computers, it is safe from being lost in the event of a hard drive failure or the like.  I do make backups of the file but only around every month or so.  I'm sure there are others that probably do the same thing as I do and I have a warning for those people... Backup your vital files (like password databases) somewhere else besides on the cloud.  Here's why:</p>
<p>I logged onto my computer late Friday night from my laptop.  I decided I was going to login to one of my blogs and start writing a new article.  Upon logging on I saw that faithful message from Dropbox in my notification area, “2 new files were synchronized successfully”.  I proceeded to fire up KeePassX and type in my password and was greeted with the following error:</p>
<p style="font-style: italic; padding-left: 1em; padding-right: 1em; font-size: 1.1em; margin-top: 1.5em; margin-bottom: 1.5em; border-left: 4px solid #333;">"The structure of the current database file is invalid. This can either happen if the user-supplied composite key  is invalid or if the file is corrupted."</p>
<p>At first, I had thought that I entered my password wrong.  So I tried again, typing more carefully this time.  <em>Same error.</em>  Then I thought maybe there was an issue with my key file, so I restored one from backup and tried again.  <em>Same error.</em>  I'm sitting in front of my laptop in disbelief, wondering what the hell I'm going to do if I can't access the hundreds of password in that file... some of which have no method of recovery...]]></description>
			<content:encoded><![CDATA[<p><i><strong>Note:</strong> Wow, I just realized that Dropbox has the ability to retrieve previous versions of files through the web interface.  This pretty much renders the post below useless, but nonetheless I&#8217;m going to leave it here because I believe the message is still important.  One backup is never enough!</i></p>
<p>This weekend I had somewhat of an unsettling experience with <a href="https://www.dropbox.com/" target="_blank">Dropbox</a>.  Typically I keep my <a href="http://keepass.info/" target="_blank">KeePass database</a> stored within Dropbox so that it is automatically synchronized between all of my computers.  I&#8217;ve felt quite confident that, because the file is synchronized to multiple computers, it is safe from being lost in the event of a hard drive failure or the like.  I do make backups of the file but only around every month or so.  I&#8217;m sure there are others that probably do the same thing as I do and I have a warning for those people&#8230; Backup your vital files (like password databases) somewhere else besides on the cloud.  Here&#8217;s why:</p>
<p>I logged onto my computer late Friday night from my laptop.  I decided I was going to login to one of my blogs and start writing a new article.  Upon logging on I saw that faithful message from Dropbox in my notification area, “2 new files were synchronized successfully”.  I proceeded to fire up KeePassX and type in my password and was greeted with the following error:</p>
<p style="font-style: italic; padding-left: 1em; padding-right: 1em; font-size: 1.1em; margin-top: 1.5em; margin-bottom: 1.5em; border-left: 4px solid #333;">&#8220;The structure of the current database file is invalid. This can either happen if the user-supplied composite key  is invalid or if the file is corrupted.&#8221;</p>
<p>At first, I had thought that I entered my password wrong.  So I tried again, typing more carefully this time.  <em>Same error.</em>  Then I thought maybe there was an issue with my key file, so I restored one from backup and tried again.  <em>Same error.</em>  I&#8217;m sitting in front of my laptop in disbelief, wondering what the hell I&#8217;m going to do if I can&#8217;t access the hundreds of password in that file&#8230; some of which have no method of recovery.  I ran downstairs and logged into my computer there only to see the same “2 new files were synchronized successfully” notification.  CRAP!  Had I been thinking I would have disconnected the network cable to make sure the files couldn&#8217;t synchronize.  Of course I was getting the same error on that computer.  I had only one other computer that my dropbox synchs to, and that computer is always on.  I knew that all of those files were going to give me the same error.  I still had a backup from a few weeks ago, but some critical passwords had changed that I would be unable to retrieve.</p>
<p>This story isn&#8217;t all bad though.  I was able to google the error and find a <a href="http://keepass.info/help/base/repair.html" target="_blank">Repair Instructions</a> for my KeePass database that got it back up and running.  However, this could&#8217;ve ended a lot worse.  I&#8217;d probably have a hard time explaining to my boss that I can&#8217;t do my job because I lost my passwords.  Lesson learned.  Everytime I make a critical password change in my database, I back it up <strong>twice</strong> to two different  <strong>secure</strong> locations where I&#8217;m confident that these files will be safe.  It is a little bit of a pain to do, but is absolutely worth it if I am ever faced with a similar situation where I cannot repair the database.</p>
<p>I really hope this brings light to anyone who keeps important data synchronized in the cloud via Dropbox or other services.  Just because your files are copied onto multiple computers does <strong>not</strong> mean that they are safe from corruption or data loss.  Make a backup.  Do it now.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.laneolson.ca/2010/02/08/critical-files-on-dropbox-a-cautionary-tale/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Beginning CakePHP: The MVC Mindset</title>
		<link>http://www.laneolson.ca/2008/12/08/beginning-cakephp-the-mvc-mindset/</link>
		<comments>http://www.laneolson.ca/2008/12/08/beginning-cakephp-the-mvc-mindset/#comments</comments>
		<pubDate>Mon, 08 Dec 2008 20:35:59 +0000</pubDate>
		<dc:creator>Lane</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://laneolsonblog.wordpress.com/?p=3</guid>
		<description><![CDATA[The manual for CakePHP has a pretty basic explanation of what MVC is.  This article will explain things in a little more detail because the MVC pattern is crucial when it comes to using CakePHP.  If you are not familiar with the MVC design pattern, read on...]]></description>
			<content:encoded><![CDATA[<p>The manual for CakePHP has a <a href="http://book.cakephp.org/view/10/Understanding-Model-View-Controller" target="_blank">pretty basic explanation</a> of what MVC is.  This article will explain things in a little more detail because the MVC pattern is crucial when it comes to using CakePHP.  If you are not familiar with the MVC design pattern, read on&#8230;</p>
<h3>What is MVC anyway?</h3>
<p>MVC stands for Model-View-Controller.  MVC is a well known design pattern that basically keeps that data handling, application flow, and presentation seperate from each other.  At first it may seem a little confusing, but it makes coding much easier and efficient.  I will explain each aspect of the MVC below relative to a simple application to manage contacts.</p>
<h4>The Model</h4>
<p>The model is responsible for handling all of the data.  In CakePHP each model usually corresponds to a table in the database.  So if in our database we have a table named &#8220;contacts&#8221; to store all of our contacts then there will be a model named &#8220;Contact&#8221;.  The plural and singular convention is important but I will not be explaining it at this time.  I recommend you read the <a href="http://book.cakephp.org/complete/22/CakePHP-Conventions">CakePHP conventions</a> before you actually start using Cake because they will save you time and headaches.  You can just trust me that the model name should be singular, and the database table should be plural.  Here is a rough example of what a model would look like in CakePHP.  This file would be stored at &#8220;/app/models/contact.php&#8221;:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #000000; font-weight: bold;">class</span> Contact <span style="color: #000000; font-weight: bold;">extends</span> AppModel 
<span style="color: #009900;">&#123;</span> 
     <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Contact'</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This is actually all you need to get started with a basic model in CakePHP.  As you can see the class Contact extends the class AppModel so it inherits all of the functions that AppModel has access to.  The AppModel has functions like find, save, delete, etc that allow you to interact with the database.  The model should be responsible for all interaction with the database.</p>
<p>So in review, anything to do with manipulating data should be done in the model.  Whether it is a query to CRUD (create, read, update, delete) operations to the database you should be doing it from the model.  Extending Cake&#8217;s AppModel class allows you to use the built in functions for CRUD operations so that you should never have to write an SQL query again!  If you would like to learn more about the model and the functions available to it check out the <a href="http://book.cakephp.org/complete/66/Models" target="_blank">Models page in the Cookbook</a>.</p>
<h4>The View</h4>
<p>The view is what it sounds like.  It is the presentation layer, everything that has to do with displaying the data is done in the view.  In cakePHP the view is set up in templates that displays data that is obtained from the model.  The view should be the <strong>only</strong> place where HTML resides.  This makes things a lot easier when it comes to modifying the look and feel of an application without having to touch the guts.  The view is basically a template and in CakePHP they have the extension &#8220;ctp&#8221;.  Here is what and example of what the view would look like to display a list of contacts&#8230; lets call it &#8220;index.ctp&#8221;.  In an application setting this file would be stored in the folder &#8220;/app/views/contacts&#8221;.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&lt;div class=&quot;contacts&quot;&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$contacts</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$contact</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;span class=&quot;contact-name&quot;&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$contact</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Contact'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/span&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/div&gt;</pre></td></tr></table></div>

<p>Of course this is an extremely basic example of printing out a list of contact names.  In the view we would definitely want to present more information in a nicer way&#8230; the best part is in order to do that the only file we will have to edit is the view.  You&#8217;re probably wondering where the $contacts variable came from, this brings us to the controller.</p>
<h4>The Controller</h4>
<p>Lastly, there is the controller.  The controller manages the flow of the application.  It is responsible for handling requests and passing the proper information for the given request.  The controller consists of several functions, called &#8220;actions&#8221;, which determine what should be done.  Each action (usually) has an associated view.  So for example, if we have an action called &#8220;index&#8221; in our controller we will need a view called &#8220;index.ctp&#8221; that displays the information for the given action.  The view in the example above uses a variable called $contact.  That variable is set using the controller.  Here is an example that ties in with the above view, this file would be &#8220;/app/controllers/contacts_controller.php&#8221; in an application setting.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> ContactsController <span style="color: #000000; font-weight: bold;">extends</span> AppController
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Contacts'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">function</span> index<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$contacts</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Contact</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'all'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'contacts'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The function in the controller above asks the Contact model to find all of the entries in the associated contacts table and then assigns it to the variable $contacts.   Afterwards the controller sets the variable $contacts so that the view can now use the $contacts variable.  The built in libraries will automatically search for the file &#8220;/app/views/contacts/index.ctp&#8221; and use it to display the information passed. The best convention is to keep the code in the controllers to a minimum and instead put the bulk of the code for manipulating the data into the model (see <a href="http://www.littlehart.net/atthekeyboard/2007/04/27/fat-models-skinny-controllers/" target="_blank">Fat Models, Skinny Controllers</a> by Chris Hartjes).</p>
<h2>Putting It All Together</h2>
<p>So there you have it, we just created an extremely simple Model, View, and Controller for listing contacts.  Of course from here you would build on creating actions in the controller for add, view, edit, etc. and views to go with those actions.  But hopefully this gives you some insight in to how it works.  If you have never followed this design practice before you are probably wondering some of the <a href="http://book.cakephp.org/view/12/Benefits">benefits of the MVC</a> practice.  It may not be immediately apparent, but there are several.  First of all, separating code in this method allows the basic functionality of the code to be reused over and over again.  When it comes to modifying the way the application looks you only have to edit the views instead of all of the code.  This allows someone who is familiar with HTML to customize the look of things without having to search endless lines of code that may not be familiar to them.  It also makes debugging much easier because you can pinpoint exactly where the problems may be coming from.  If the data isn&#8217;t correct, it must be the model&#8230; If something is funky with the way it is displayed, it has got to be the view&#8230; and so on.  This is of course just one of the great design patterns used by Cake and there are several others that will save time and headaches.</p>
<h2>Additional MVC Articles</h2>
<ul>
<li><a href="http://en.wikipedia.org/wiki/Model-view-controller#PHP" target="_blank">MVC on Wikipedia </a></li>
<li><a href="http://book.cakephp.org/complete/10/Understanding-Model-View-Controller" target="_blank">The CakePHP Manual MVC documentation</a></li>
<li><a href="http://book.cakephp.org/complete/66/Models" target="_blank">Cookbook entry for Models</a></li>
<li><a href="http://book.cakephp.org/complete/94/Views" target="_blank">Cookbook entry for Views</a></li>
<li><a href="http://book.cakephp.org/complete/49/Controllers" target="_blank">Cookbook entry for Controllers</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.laneolson.ca/2008/12/08/beginning-cakephp-the-mvc-mindset/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
