<?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>CodeSanity &#187; crud</title>
	<atom:link href="http://codesanity.net/tag/crud/feed/" rel="self" type="application/rss+xml" />
	<link>http://codesanity.net</link>
	<description>PHP, Javascript &#38; Technology Ramblings</description>
	<lastBuildDate>Wed, 07 Jul 2010 00:28:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How to Structure Models in your MVC based Web Application</title>
		<link>http://codesanity.net/2009/11/structure-models-mvc-based-web-application/</link>
		<comments>http://codesanity.net/2009/11/structure-models-mvc-based-web-application/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 05:01:20 +0000</pubDate>
		<dc:creator>Tom Schlick</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[active record]]></category>
		<category><![CDATA[applications architecture]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[crud]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[view]]></category>

		<guid isPermaLink="false">http://codesanity.net/?p=99</guid>
		<description><![CDATA[MVC has defiantly changed the way applications are written over the past few years. It is more organized and developer-friendly, but it comes with a bit of a learning curve. I will cover what models are and how I use them to speed up my application development.]]></description>
			<content:encoded><![CDATA[<h1>MVC</h1>
<p>Ever since I started using <a href="http://codeigniter.com">CodeIgniter</a> about a year and a half ago, I feel that my coding style has drastically improved as well as the speed, security, and reliability of my applications. This is mostly due to CodeIgniter&#8217;s <a href="http://en.wikipedia.org/wiki/Model-view-controller">MVC</a> approach to the structure of the application. It separates your code into three stages:</p>
<ul>
<li>Model &#8211; Used to interact with your database or data source and return the data back to your application</li>
<li>View &#8211; Holds all of the display logic such as html, css and javascript</li>
<li>Controller &#8211; Sort of the middleman, used to coordinate all the actions from Models and Libraries into a View to display to the user&#8217;s browser</li>
</ul>
<h1>CRUD</h1>
<p><a href="http://en.wikipedia.org/wiki/Create,_read,_update_and_delete">CRUD (Create, read, update and delete)</a> is a way of structuring your code that interfaces with your database. Those four actions handle 99% of the actions you will do with the data in your database so why should we duplicate functionality every time we need to do something? For my examples I will be using the <a href="http://codeigniter.com/user_guide/database/active_record.html">Active Record Class</a> in CodeIgniter but it would be just as easy for you to use simple SQL statements in your PHP code.</p>
<p>Ok that&#8217;s enough of the vocabulary lesson, lets get into some code.</p>
<h1>Start of the Model</h1>
<p><script src="http://gist.github.com/464047.js?file=gistfile1.php"></script></p>
<p>All we are doing here is declaring the model as &#8220;Blog_model&#8221; and we will save this code in blog_model.php in the models folder in Codeigniter.</p>
<h1>Create</h1>
<p>Creating data is pretty simple, especially in codeigniter. You should only need a single function to create data for each table in your database. This is how i structure my insert functions.</p>
<p><script src="http://gist.github.com/464050.js?file=gistfile1.php"></script></p>
<p>Now what the code above is actually doing is this. When i call $this-&gt;whatever_model-&gt;insert_blog_post($data); , i pass a $data variable which is actually just an array with the index names matching to the database field names. If you are familiar with <a href="http://codeigniter.com/user_guide/database/active_record.html" target="_blank">CodeIgniter&#8217;s Active Record Class</a> this approach is nothing new to you.</p>
<h1>Read</h1>
<p>Reading information from a database is nothing new. You send a select query to a database and it returns results, how could it get any simpler? Well what usually ends up happening over the development of an application is that the developer will create multiple functions that essentially do the same thing. One function will query the database based on the Unique ID # of an item. Another will query checking to see if a particular title in that blog post already exists. So how can these be combined? Well you can create a single function that actually interacts with the database. It is passed a few parameters that tell it what query to run and it does just that. This function is called by other functions that pass those specific parameters. Here is an example.</p>
<p><script src="http://gist.github.com/464053.js?file=gistfile1.php"></script></p>
<h1>Update</h1>
<p>Now updating an row (or many rows) in the database is just as easy as adding a row. The first parameter is your where statement which includes the conditions that the rows must have to be updates. The second parameter is the data that will be inserted into those selected rows.</p>
<p><script src="http://gist.github.com/464055.js?file=gistfile1.php"></script></p>
<p>Pretty simple right? Now all you have to do from anywhere in your application to update a row in blog_posts is $this-&gt;blog_model-&gt;update_blog_post(array(&#8216;ID&#8217; =&gt; 5), array(&#8216;title&#8217; =&gt; &#8216;Getting Started with CRUD&#8217;));</p>
<h1>Delete</h1>
<p>Just as you might suspect, deleting a post is just as simple as adding or updating a post.</p>
<p><script src="http://gist.github.com/464058.js?file=gistfile1.php"></script></p>
<p>Now just run the command $this-&gt;blog_model-&gt;delete_blog_post(array(&#8216;ID&#8217; =&gt; &#8217;223&#8242;));</p>
<h2>Thats it!</h2>
<p>If you have any questions or comments be sure to leave them below!</p>
]]></content:encoded>
			<wfw:commentRss>http://codesanity.net/2009/11/structure-models-mvc-based-web-application/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)
Content Delivery Network via Amazon Web Services: S3: assets.codesanity.net.s3.amazonaws.com

Served from: codesanity.net @ 2010-09-06 06:54:19 -->