<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Web Technology: A Revolution</title>
	<atom:link href="http://webtechnology.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://webtechnology.wordpress.com</link>
	<description>Developed by Aseef Ahmed</description>
	<lastBuildDate>Fri, 12 Aug 2011 09:48:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='webtechnology.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Web Technology: A Revolution</title>
		<link>http://webtechnology.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://webtechnology.wordpress.com/osd.xml" title="Web Technology: A Revolution" />
	<atom:link rel='hub' href='http://webtechnology.wordpress.com/?pushpress=hub'/>
		<item>
		<title>What goes on debit side &amp; What goes on credit side?</title>
		<link>http://webtechnology.wordpress.com/2011/08/12/what-goes-on-debit-side-what-goes-on-credit-side/</link>
		<comments>http://webtechnology.wordpress.com/2011/08/12/what-goes-on-debit-side-what-goes-on-credit-side/#comments</comments>
		<pubDate>Fri, 12 Aug 2011 09:36:52 +0000</pubDate>
		<dc:creator>aseefahmed</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://webtechnology.wordpress.com/?p=124</guid>
		<description><![CDATA[Here is the general rule to remember which side is debit and which side is credit: var odesk_widgets_width = 340; var odesk_widgets_height = 230; To increase purchases/expenses/assets accounts, post a debit entry To decrease purchases/expenses/assets accounts, post a credit entry To increase purchases/expenses/assets accounts, post a debit entry To increase income/liabilities/equity accounts, post a credit [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&amp;blog=2417486&amp;post=124&amp;subd=webtechnology&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Here is the general rule to remember which side is debit and which side is credit:</p>
<p><!-- START Hire Me on oDesk Widget --></p>
<p>var odesk_widgets_width = 340;<br />
var odesk_widgets_height = 230;</p>
<p><!-- END Hire Me on oDesk Widget --></p>
<ul>
<li>To increase purchases/expenses/assets accounts, post a debit entry</li>
<li>To decrease purchases/expenses/assets accounts, post a credit entry</li>
<li>To increase purchases/expenses/assets accounts, post a debit entry</li>
<li>To increase income/liabilities/equity accounts, post a credit entry</li>
<li>To decrease income/liabilities/equity accounts, post a debit entry</li>
</ul>
<p>in the following table, it has been illustrated which accounts are debit entry &amp; which accounts are credit entries:</p>
<table border='1'>
<tr>
<td>Debit Entry</td>
<td>Credit Entry</td>
</tr>
<tr>
<td>
<ul>
<li>Purchases</li>
<li>Expenses</li>
<li>Assets</li>
</ul>
</td>
<td>
<ul>
<li>Income</li>
<li>Liabilities</li>
<li>Equity</li>
</ul>
</td>
</tr>
</table>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webtechnology.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webtechnology.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webtechnology.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webtechnology.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/webtechnology.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/webtechnology.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/webtechnology.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/webtechnology.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webtechnology.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webtechnology.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webtechnology.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webtechnology.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webtechnology.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webtechnology.wordpress.com/124/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&amp;blog=2417486&amp;post=124&amp;subd=webtechnology&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://webtechnology.wordpress.com/2011/08/12/what-goes-on-debit-side-what-goes-on-credit-side/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4682697a6bd2ad125716a325e07284a2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aseefahmed</media:title>
		</media:content>
	</item>
		<item>
		<title>Amazon product advertising API through which you can earn money</title>
		<link>http://webtechnology.wordpress.com/2009/12/27/amazon-product-advertising-api-through-which-you-can-earn-money/</link>
		<comments>http://webtechnology.wordpress.com/2009/12/27/amazon-product-advertising-api-through-which-you-can-earn-money/#comments</comments>
		<pubDate>Sun, 27 Dec 2009 19:41:03 +0000</pubDate>
		<dc:creator>aseefahmed</dc:creator>
				<category><![CDATA[Amazon]]></category>

		<guid isPermaLink="false">http://webtechnology.wordpress.com/?p=82</guid>
		<description><![CDATA[Amazon has spent over ten years and hundreds of millions of dollars developing a world-class web service that millions of customers use every day. As a developer, you can build Product Advertising API applications that leverage this robust, scalable, and reliable technology. You get access to much of the data that is used by Amazon, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&amp;blog=2417486&amp;post=82&amp;subd=webtechnology&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div style="text-align:justify;">
</div>
<div style="text-align:justify;">
</div>
<div style="text-align:justify;">
Amazon has spent over ten years and hundreds of millions of dollars developing a world-class web service that millions of customers use every day. As a developer, you can build Product Advertising API applications that leverage this robust, scalable, and reliable technology. You get access to much of the data that is used by Amazon, including the items for sale, customer reviews, seller reviews, as well as most of the functionality that you see on www.amazon.com, such as finding items, finding similar items, displaying customer reviews, and product promotions. In short, Product Advertising API operations open the doors to Amazon&#8217;s databasies so that you can take advantage of Amazon&#8217;s sophisticated e-commerce data and functionality. This API will give you a chance to build your own web store to sell Amazon items and you will earn. This API is free. By signing up to become a Product Advertising API developer, you join the tens of thousands of developers who are already realizing financial gains by creating Product Advertising API-driven applications and web stores.<span style="color:#741b47;">(Source: Amazon website)</span>
</div>
<p><BR></p>
<div style="text-align:justify;">
</div>
<div style="text-align:justify;">
<span style="color:red;font-size:x-large;">How does this API work</span>
</div>
<ol>
<li>
<div style="text-align:justify;">
Your website uses the Product Advertising API which supplies item descriptions and images, and customer and seller reviews from Amazon database, which you present to your customers.</div>
</li>
<li>
<div style="text-align:justify;">
Customers shop on your web site.</div>
</li>
<li>
<div style="text-align:justify;">
When the customer is ready to purchase the items in their e-commerce shopping cart,your application sends an HTML form to Product Advertising API and Amazon completesthe purchase by getting purchase information, such as payment method and shipping address, and then Amazon fulfills the order by shipping the items.</div>
</li>
</ol>
<p><span style="color:#274e13;">To make money using the Product Advertising API to advertise Amazon products in conjunction with the Amazon Associates program, you need to register to become a Product Advertising API developer and join the Amazon Associates Program.</span></p>
<div style="text-align:justify;">
<span style="color:red;font-size:x-large;">Getting Your Key Pair</span>
</div>
<div style="text-align:justify;">
</div>
<div style="text-align:justify;">
</div>
<div style="text-align:justify;">
Once you have registered as an Amazon Associate, you will need a key pair, consisiting of an access Key ID and a secret key. The Product Advertising API uses the Amazon Web Services infrastructure for generating key pairs, as well as for other Product Advertising API resources. You can get your keys at <br /><a href="http://aws-portal.amazon.com/gp/aws/developer/account/index.html?action=access-key.">http://aws-portal.amazon.com/gp/aws/developer/account/index.html?action=access-key.</a>&nbsp;Once you have obtained your key pair, you can make calls to the Product Advertising API.
</div>
<p><span style="color:#cc0000;" align="right"><strong>[Download]</strong></span><br />
<a href="http://webtechnology.files.wordpress.com/2009/12/prod-adv-api-gsg-20091001.pdf" title="Getting started guide">Getting Started Guide</a><br />
<a href="http://webtechnology.files.wordpress.com/2011/08/prod-adv-api-dg-20091001.pdf" title="Developer guide">Developer Guid</a>e</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webtechnology.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webtechnology.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webtechnology.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webtechnology.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/webtechnology.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/webtechnology.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/webtechnology.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/webtechnology.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webtechnology.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webtechnology.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webtechnology.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webtechnology.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webtechnology.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webtechnology.wordpress.com/82/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&amp;blog=2417486&amp;post=82&amp;subd=webtechnology&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://webtechnology.wordpress.com/2009/12/27/amazon-product-advertising-api-through-which-you-can-earn-money/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4682697a6bd2ad125716a325e07284a2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aseefahmed</media:title>
		</media:content>
	</item>
		<item>
		<title>DYNAMIC WEBSITE HOSTING(ABSOLUTELY FREE)</title>
		<link>http://webtechnology.wordpress.com/2009/09/06/dynamic-website-hostingabsolutely-free/</link>
		<comments>http://webtechnology.wordpress.com/2009/09/06/dynamic-website-hostingabsolutely-free/#comments</comments>
		<pubDate>Sun, 06 Sep 2009 17:34:07 +0000</pubDate>
		<dc:creator>aseefahmed</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://webtechnology.wordpress.com/?p=62</guid>
		<description><![CDATA[Free cPanel Web Hosting with PHP5/Mysql &#8211; no advertising! Register now: http://www.000webhost.com/200843.html We can offer you a free web hosting package packed with advanced features for hosting &#38; building professional dynamic websites. We provide secure free web space with all the web hosting tools you could possibly ever need. Our package includes: - 1500 MB [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&amp;blog=2417486&amp;post=62&amp;subd=webtechnology&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.support.wordpress.com/affiliate-links/"><img src="http://www.000webhost.com/images/banners/468x60/banner13.gif" alt="Free Website Hosting" width="468" height="60" border="0" /></a></p>
<p>Free cPanel Web Hosting with PHP5/Mysql &#8211; no advertising!<br />
Register now: http://www.000webhost.com/200843.html</p>
<p>We can offer you a free web hosting package packed with advanced features for hosting &amp; building professional dynamic websites. We provide secure free web space with all the web hosting tools you could possibly ever need.</p>
<p>Our package includes:<br />
- 1500 MB of Disk Space, 100 GB Bandwidth<br />
- Host your own domain (http://www.yourdomain.com)<br />
- cPanel Powered Hosting (you will love it)<br />
- Over 500 website templates ready to download<br />
- Easy to use website builder<br />
- Free POP3 Email Box with Webmail access<br />
- FTP and Web based File Manager<br />
- PHP, MySQL, Perl, CGI, Ruby.<br />
- And many more..</p>
<p><a href="http://en.support.wordpress.com/affiliate-links/"><img src="http://www.000webhost.com/images/banners/600x400/banner1.gif" alt="Free Website Hosting" width="550" height="400" border="0" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webtechnology.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webtechnology.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webtechnology.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webtechnology.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/webtechnology.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/webtechnology.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/webtechnology.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/webtechnology.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webtechnology.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webtechnology.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webtechnology.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webtechnology.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webtechnology.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webtechnology.wordpress.com/62/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&amp;blog=2417486&amp;post=62&amp;subd=webtechnology&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://webtechnology.wordpress.com/2009/09/06/dynamic-website-hostingabsolutely-free/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4682697a6bd2ad125716a325e07284a2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aseefahmed</media:title>
		</media:content>

		<media:content url="http://www.000webhost.com/images/banners/468x60/banner13.gif" medium="image">
			<media:title type="html">Free Website Hosting</media:title>
		</media:content>

		<media:content url="http://www.000webhost.com/images/banners/600x400/banner1.gif" medium="image">
			<media:title type="html">Free Website Hosting</media:title>
		</media:content>
	</item>
		<item>
		<title>Managing Software with yum</title>
		<link>http://webtechnology.wordpress.com/2008/07/03/managing-software-with-yum/</link>
		<comments>http://webtechnology.wordpress.com/2008/07/03/managing-software-with-yum/#comments</comments>
		<pubDate>Thu, 03 Jul 2008 06:57:06 +0000</pubDate>
		<dc:creator>aseefahmed</dc:creator>
				<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://webtechnology.wordpress.com/?p=45</guid>
		<description><![CDATA[About Packages Each package is a compressed archive containing product information, program files, icons, documentation and management scripts. Management applications use these files to safely locate, install, update and remove software. Packages also include a digital signature to prove their source. Software management utilities verify this digital signature by using a GPG public key. The [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&amp;blog=2417486&amp;post=45&amp;subd=webtechnology&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="center"><a href="http://en.support.wordpress.com/affiliate-links/"><img border="0" src="http://www.hits4pay.com/banners/468X60h4p1.jpg" width="469" height="61"></a></p>
<h3 class="title">About Packages</h3>
<p>Each package is a compressed archive containing product information, program files, icons, documentation and management scripts. Management applications use these files to safely locate, install, update and remove software.</p>
<p>Packages also include a digital signature to prove their source. Software management utilities verify this digital signature by using a GPG <em>public key</em>. The <code>yum</code> and <code>rpm</code> utilities share a common <em>keyring</em> that stores all of the public keys for approved package sources. The system administrator configures these approved package sources.</p>
<h3 class="title">About Repositories</h3>
<p>A <em>repository</em> is a prepared directory or web site that contains software packages and index files. Software management utilities such as <code>yum</code> automatically locate and obtain the correct RPM packages from these repositories. This method frees you from having to manually find and install new applications or updates. You may use a single command to update all system software, or search for new software by specifying criteria.</p>
<h3 class="title">About Dependencies</h3>
<p>Some of the files installed on a distribution are <em>libraries</em> which may provide functions to multiple applications. When an application requires a specific library, the package which contains that library is a <em>dependency</em>. To properly install a package. The dependency information for a RPM package is stored within the RPM file.</p>
<p>The <code>yum</code> utility uses package dependency data to ensure that all of requirements for an application are met during installation. It automatically installs the packages for any dependencies not already present on your system. If a new application has requirements that conflict with existing software, <code>yum</code> aborts without making any changes to your system.</p>
<h3 class="title">Understanding Package Names</h3>
<p>Each package file has a long name that indicates several key pieces of information. This is the full name of the <code>tsclient</code> package supplied with CentOS:</p>
<pre><code>tsclient-0.132-4.i386.rpm</code></pre>
<p>Management utilities commonly refer to packages with one of three formats:</p>
<div class="itemizedlist">
<ul type="disc">
<li>Package name: <code>tsclient</code></li>
<li>Package name with version and release numbers: <code>tsclient-0.132-4</code></li>
<li>Package name with hardware architecture: <code>tsclient.i386</code></li>
</ul>
</div>
<p>For clarity, <code>yum</code> lists packages in the format <code>name.architecture</code>. Repositories also commonly store packages in separate directories by architecture. In each case, the hardware architecture specified for the package is the <span class="emphasis"><em>minimum</em></span> type of machine required to use the package.</p>
<p><!-- Begin BidVertiser code --></p>
<p><!-- End BidVertiser code --> </p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/webtechnology.wordpress.com/45/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/webtechnology.wordpress.com/45/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webtechnology.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webtechnology.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webtechnology.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webtechnology.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/webtechnology.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/webtechnology.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/webtechnology.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/webtechnology.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webtechnology.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webtechnology.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webtechnology.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webtechnology.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webtechnology.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webtechnology.wordpress.com/45/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&amp;blog=2417486&amp;post=45&amp;subd=webtechnology&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://webtechnology.wordpress.com/2008/07/03/managing-software-with-yum/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4682697a6bd2ad125716a325e07284a2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aseefahmed</media:title>
		</media:content>

		<media:content url="http://www.hits4pay.com/banners/468X60h4p1.jpg" medium="image" />
	</item>
		<item>
		<title>Create Your Own Mail Script With PHP and IMAP</title>
		<link>http://webtechnology.wordpress.com/2008/05/26/create-your-own-mail-script-with-php-and-imap/</link>
		<comments>http://webtechnology.wordpress.com/2008/05/26/create-your-own-mail-script-with-php-and-imap/#comments</comments>
		<pubDate>Mon, 26 May 2008 08:31:14 +0000</pubDate>
		<dc:creator>aseefahmed</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://webtechnology.wordpress.com/?p=44</guid>
		<description><![CDATA[Thanks to PHP, we can check our email account remotely using PHP and its imap_xxx functions, which allow us to communicate with mail servers via IMAP, POP3 or NNTP protocols. In this article Mitchell shows us how to create a completely web-based email checking script which can also delete, send and reply to emails&#8230; all [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&amp;blog=2417486&amp;post=44&amp;subd=webtechnology&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.support.wordpress.com/affiliate-links/"><img src="http://www.000webhost.com/images/banners/468x60/banner13.gif" alt="Free Website Hosting" width="468" height="60" border="0" /></a></p>
<p><span class="txtplain1">Thanks to PHP, we can check our email account remotely using PHP and its imap_xxx functions, which allow us to communicate with mail servers via IMAP, POP3 or NNTP protocols. In this article Mitchell shows us how to create a completely web-based email checking script which can also delete, send and reply to emails&#8230; all using only PHP&#8217;s IMAP functions.How many times have you been away from your home PC and wished that you could check your IMAP/POP3 mail account? If you&#8217;re anything like me then you&#8217;re always on the move and your laptop is your best friend.</span></p>
<p>Thanks to PHP, we can check our email account remotely using PHP and its imap_xxx functions, which allow us to communicate with mail servers via IMAP (Internet Message Access Protocol), POP3 (Post Office Protocol) and/or NNTP (Network News Transfer Protocol).</p>
<p>In this article we&#8217;re going to build a mail script that will accept an email account, password and optional IMAP port number. The script will then login to that mail account and display the users inbox in a similar fashion to the way Hotmail does: all using only PHP and HTML. From the inbox screen, we have the option to compose a new email, view our emails and even reply to them.</p>
<p>To test the source code and examples in this article you should be running either a Windows/Linux server with IIS/Apache and PHP 4+ installed. You should also have the IMAP libraries installed, which we will look at shortly.</p>
<p><span class="txtplain1"><span class="contentpagetitle"><strong>Create Your Own Mail Script With PHP and IMAP &#8211; What is IMAP?</strong></span><br />
(Page 2 of 6 )</span></p>
<p>IMAP is a method of accessing email messages stored on a remote mail server. The reason IMAP is so popular is because it allows clients to connect to and interact with their mail server without the need to download their emails and files locally. In other words, IMAP allows us to treat a remote mail server as is if it was on our local machine, checking our accounts, manipulating emails, replying to emails, etc.</p>
<p>IMAP shouldn&#8217;t be confused with the POP3 protocol, however. If you&#8217;ve ever used Outlook or Eudora or any other client mail access program, then you&#8217;ll know that you can &#8220;check&#8221; your email, disconnect from the Internet and your mail will be stored on your local machine, saving you the hassle of being on the &#8216;net to read your email. This is POP&#8217;s biggest advantage: offline access to your email. POP downloads all new messages off the server onto your local PC and then deletes them from that server. IMAP on the other hand queries the server, leaving all emails intact unless you specifically tell it to delete them.</p>
<p>POP was designed for use with client side email applications such as Outlook, whereas IMAP was designed for people on the move, wanting to check their email but not necessarily retrieve it.</p>
<p>The key goals for IMAP are:</p>
<ul>
<li>Be fully compatible with Internet messaging standards, e.g. MIME.</li>
<li>Allow message access and management from more than one computer.</li>
<li>Allow access without reliance on less efficient file access protocols.</li>
<li>Provide support for online, offline and disconnected access modes.</li>
<li>Support for concurrent access to shared mailboxes.</li>
<li>Client software needs no knowledge about the server&#8217;s file store format.</li>
</ul>
<p>Let&#8217;s take the last point into configuration: &#8220;Client software needs no knowledge about the server&#8217;s file store format&#8221;. In my opinion this is one of IMAP&#8217;s best features. The client simply queries the mail server using IMAP specific commands and that server returns the data the client&#8217;s after.</p>
<p>It doesn&#8217;t matter how the server stores the actual email messages (whether it be in a flat file, a MySQL database, an MSSQL server database or any other format) as long as the server returns the data that the client requests, then the storage method and location are irrelevant.</p>
<p><strong>Bringing PHP into the picture</strong></p>
<p>If you&#8217;ve worked with PHP extensions then you&#8217;ll know that it supports IMAP through an extension library, which isn&#8217;t installed by default. Before we can play around with PHP and IMAP, we have to install the necessary IMAP libraries first.</p>
<p><strong>Configuring PHP on Windows with IMAP support</strong></p>
<p>It&#8217;s easy to configure your Windows server to work with IMAP. Firstly, grab the php_imap.dll file from your PHP download (it should be in the extensions directory). If you can&#8217;t find it then download the PHP zip package <a rel="nofollow" href="http://www.php.net/downloads.php" target="_blank">here</a>.</p>
<p>Next, copy php_imap.dll to your PHP extension directory, which you can find in your php.ini file as the extension_dir variable. Lastly, open php.ini and look for the line starting &#8220;;extension=php_imap.dll&#8221;. Remove the semicolon from the front, save the file and restart your web server.</p>
<p><strong>Configuring PHP on Linux with IMAP support</strong></p>
<p>To enable IMAP support on your PHP installation you need to compile PHP with the &#8211;with-imap argument. You also need to download the IMAP library <a rel="nofollow" href="ftp://ftp.cac.washington.edu/imap/" target="_blank">here</a>. Once you&#8217;ve downloaded it, copy c-client/c-client.a to /usr/local/lib/libc-client.a. Also copy c-client/rfc822.h, mail.h and linkage.h to /usr/local/include or some other directory in your include path.</p>
<p><strong>Testing for IMAP support</strong></p>
<p>To make sure IMAP support is configured correctly, create a new file called phpinfo.php and save it into a directory that your web server can process. Add the following code to it and run it in your web browser:</p>
<p><span class="bodyCode">&lt;?php echo phpinfo(); ?&gt;</span></p>
<p>Scroll down the page about half way and look for the IMAP section. You should see something like this:</p>
<p><img src="http://www.devarticles.com/images/phpimap_1.gif" alt="Making sure IMAP was configured OK" align="middle" /></p>
<p>Now that we&#8217;ve setup IMAP support, it&#8217;s time to take a look at the various IMAP functions that we will be using later in this article to create our email script.</p>
<p><span class="txtplain1"><span class="contentpagetitle"><strong>Create Your Own Mail Script With PHP and IMAP &#8211; IMAP functions</strong></span><br />
(Page 3 of 6 )</span></p>
<p>The IMAP library has 61 functions. Luckily we only need to use a handful of these to create a fully functional email script. The functions we will be using in this article are shown and detailed below:</p>
<ul>
<li><strong>imap_open:</strong> Opens an IMAP stream to a mailbox.</li>
<li><strong>imap_headers:</strong> Returns headers for all emails in a mailbox.</li>
<li><strong>imap_headerinfo:</strong> Reads the header of an email.</li>
<li><strong>imap_delete:</strong> Mark an email for deletion from current mailbox.</li>
<li><strong>imap_expunge:</strong> Delete all emails marked for deletion.</li>
<li><strong>imap_mail:</strong> Sends an email message to a recipient.</li>
<li><strong>imap_body:</strong> Reads the body of an email from a mailbox.</li>
</ul>
<p>As mentioned earlier, we&#8217;re going to create a script that allows us to check our email account on the move using IMAP. Before we create the script however, let&#8217;s start with the basics. Create a new file called testimap.php and enter the following code into it:</p>
<p><span class="bodyCode">&lt;?php</span></p>
<p>$server = &#8220;mail.com&#8221;;</p>
<p>$user = &#8220;john&#8221;;</p>
<p>$pass = &#8220;mypass&#8221;;</p>
<p>$conn = @imap_open(&#8220;\{$server/imap}INBOX&#8221;, $user, $pass)</p>
<p>or die(&#8220;Connection to server failed&#8221;);</p>
<p>?&gt;</p>
<p>Replace the $server variable with the host name of your mail server. Replace $user with your mailbox account name and also replace $pass with the password for your mailbox. To connect to our mail server we use the imap_open function, passing in our mailbox, user ID and password. The format of the mailbox parameter looks like this:</p>
<p>{host_name[:port number]/imap}INBOX</p>
<p>The port number part of the mailbox parameter is optional. If we refer back to the testimap.php file above, our mailbox parameter looks like this:</p>
<p>{mail.com/imap}MAILBOX</p>
<p>In our example we&#8217;ve left out the port number. We could&#8217;ve optionally specified the port number like this:</p>
<p>{mail.com:25/imap}INBOX</p>
<p>The imap_open function returns an IMAP stream on success and FALSE on failure. We use this returned IMAP stream with various other imap_xxx function calls to manipulate our mailbox, send emails, etc.</p>
<p>To retrieve a list of emails in our mailbox, we can use the following code:</p>
<p><span class="bodyCode">$headers = @imap_headers($conn)</span></p>
<p>or die(&#8220;Couldn&#8217;t get emails&#8221;);</p>
<p>$numEmails = sizeof($headers);</p>
<p>echo &#8220;You have $numEmails in your mailbox&#8221;;</p>
<p>In the example above I&#8217;ve called the imap_headers function, passing in our IMAP stream as the only parameter. The imap_headers function returns an array of headers. Each of these headers contains the details of one email in our mailbox, and we can use the values of these headers to show who these emails have come from, their subjects, date received, etc. These headers do not however contain the body of each email message, and we must use the imap_body function to get them.</p>
<p>Now that we&#8217;ve got an array of headers representing each email in our mailbox, we can loop through them to display the details of each message with the imap_headerinfo function, like this:</p>
<p><span class="bodyCode">for($i = 1; $i &lt; $numEmails+1; $i++)</span></p>
<p>{</p>
<p>$mailHeader = @imap_headerinfo($conn, $i);</p>
<p>$from = $mailHeader-&gt;fromaddress;</p>
<p>$subject = strip_tags($mailHeader-&gt;subject);</p>
<p>$date = $mailHeader-&gt;date;</p>
<p>echo &#8220;Email from $from, subject $subject, date $date&lt;br&gt;&#8221;;</p>
<p>}</p>
<p>As you can see from the code above, we use the imap_headerinfo function to get the header details for each message. We extract the from address, subject and date for each message and use the echo command to output them to the browser.</p>
<p>Now that we&#8217;ve displayed each message in our mailbox, we can use the imap_close function to close the IMAP stream we created earlier with imap_open:</p>
<p><span class="bodyCode">imap_close($conn);</span></p>
<p>On this page we&#8217;ve taken a look at the basic imap_xxx functions that are required to open an IMAP stream, retrieve headers and display the details of each email from these headers. On the next page we&#8217;re going to put everything we&#8217;ve learnt so far to good use and create the email script I&#8217;ve been mentioning.</p>
<p><span class="contentpagetitle"><strong>Create Your Own Mail Script With PHP and IMAP &#8211; Creating our email script</strong></span><br />
(Page 4 of 6 )</p>
<p>There are a huge number of email applications on the market, however most of them expose the same core functionality:</p>
<ul>
<li>You can check your mailbox on any mail server.</li>
<li>You can create and send emails.</li>
<li>You can reply to emails.</li>
<li>You can delete emails from your mailbox.</li>
</ul>
<p>The script we&#8217;re about to discuss implements each of the bullet points shown above. We will run through the code step by step, describing various function calls where necessary.</p>
<p>Our script contains several functions and a switch statement. The switch statement works with the $what variable, which can be either a query string or form posted variable. Each time our script is called, a different function is executed, depending on the value of $what:</p>
<p><span class="bodyCode">switch($what)</span></p>
<p>{</p>
<p>case &#8220;login&#8221;:</p>
<p>ProcessLogin();</p>
<p>break;</p>
<p>case &#8220;inbox&#8221;:</p>
<p>ShowInbox();</p>
<p>break;</p>
<p>case &#8220;delete&#8221;:</p>
<p>DeleteEmails();</p>
<p>break;</p>
<p>case &#8220;compose&#8221;:</p>
<p>ShowComposeForm();</p>
<p>break;</p>
<p>case &#8220;send&#8221;:</p>
<p>SendMessage($to, $subject, $message, $cc, $bcc);</p>
<p>break;</p>
<p>case &#8220;read&#8221;:</p>
<p>ShowEmail();</p>
<p>break;</p>
<p>case &#8220;reply&#8221;:</p>
<p>ShowComposeForm($to, $subject, $message);</p>
<p>break;</p>
<p>default:</p>
<p>ShowLoginForm();</p>
<p>}</p>
<p>As you can see, the default option calls the ShowLoginForm function, which allows us to enter our email address and password. The ShowLoginForm function simply displays a HTML form which includes a hidden form variable, $what, which is set to &#8220;login&#8221;.</p>
<p>The ProcessLogin function is called when $what is equal to &#8220;login&#8221;. It validates the email address from the login form using the ereg function and creates some session variables containing our mailbox, username, host and password:</p>
<p><span class="bodyCode">$validEmail = ereg(&#8220;^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$&#8221;, $email);</span></p>
<p>$validPass = ($password == &#8220;&#8221; ? false: true);</p>
<p>$portString = (is_numeric($port) == true ? &#8220;:$port&#8221; : &#8220;&#8221;);</p>
<p>if(!$validEmail || !$validPass)</p>
<p>{</p>
<p>// Email or password are bad</p>
<p>die(&#8220;&lt;h2&gt;Invalid Login Credentials&lt;/h2&gt;&#8221;);</p>
<p>}</p>
<p>else</p>
<p>{</p>
<p>// Email and password are fine, get the user and hosts</p>
<p>$arr = split(&#8220;@&#8221;, $email);</p>
<p>$user = $arr[0];</p>
<p>$host = $arr[1];</p>
<p>$mailbox = &#8220;\{$host$portString/imap}INBOX&#8221;;</p>
<p>// Set the login credentials as session variables</p>
<p>setcookie(&#8220;mailbox&#8221;, $mailbox);</p>
<p>setcookie(&#8220;user&#8221;, $user);</p>
<p>setcookie(&#8220;host&#8221;, $host);</p>
<p>setcookie(&#8220;password&#8221;, $password);</p>
<p>header(&#8220;Location: $PHP_SELF?what=inbox&#8221;);</p>
<p>}</p>
<p>As you can see, we use the split function to separate the users email address into the $user and $host variables. We also create the $mailbox variable from the host name and port number, which may or may not be specified in the login form. The user is then redirected to the same scripting using $PHP_SELF, passing &#8220;inbox&#8221; as the $what query string variable. This results in the ShowInbox function being called.</p>
<p>The ShowInbox function connects to our mail server using imap_open (which we saw in our first example) and retrieves the headers for each email message in our mailbox. These headers are then extracted into variables and a HTML table is created that lists the details of each email. Each email is also hyper linked to the script, setting the query variables $what to &#8220;read&#8221;, and $id to the ID of the email, which is the index of that particular email in the array returned from a call to imap_headers:</p>
<p><span class="bodyCode">&lt;td width=&#8221;50%&#8221;&gt;</span></p>
<p>&lt;a href=&#8221;&lt;?php echo $PHP_SELF; ?&gt;?what=read&amp;id=&lt;?php echo $i; ?&gt;&#8221;&gt;</p>
<p>&lt;?php echo $subject; ?&gt;</p>
<p>&lt;/a&gt;</p>
<p>&lt;/td&gt;</p>
<p>When $what is &#8220;read&#8221;, the ShowEmail function is called.</p>
<p><span class="txtplain1"><span class="contentpagetitle"><strong>Create Your Own Mail Script With PHP and IMAP &#8211; The ShowEmail function and more</strong></span><br />
(Page 5 of 6 )</span></p>
<p>ShowEmail connects to our IMAP server using imap_open and retrieves both the header and body details of one specific email. As we saw earlier, imap_headerinfo returns the header details of an email as an array, like this:</p>
<p><span class="bodyCode">$mailHeader = @imap_headerinfo($conn, $id);</span></p>
<p>&#8230;</p>
<p>$from = $mailHeader-&gt;fromaddress;</p>
<p>$subject = strip_tags($mailHeader-&gt;subject);</p>
<p>$date = $mailHeader-&gt;date;</p>
<p>The body of the email is then retrieved with a call to imap_body. Because our email script only displays plain text emails, we use strip_tags to remove all HTML formatting from the email:</p>
<p><span class="bodyCode">$body = nl2br(strip_tags(imap_body($conn, $id)));</span></p>
<p>We then output all of these details to a form, including a button that lets us reply to this specific email:</p>
<p><span class="bodyCode">&lt;form name=&#8221;frmLogin&#8221; action=&#8221;&lt;?php echo $PHP_SELF; ?&gt;&#8221; method=&#8221;post&#8221;&gt;</span></p>
<p>&lt;input type=&#8221;hidden&#8221; name=&#8221;what&#8221; value=&#8221;reply&#8221;&gt;</p>
<p>&lt;input type=&#8221;hidden&#8221; name=&#8221;to&#8221; value=&#8221;&lt;?php echo $from; ?&gt;&#8221;&gt;</p>
<p>&lt;input type=&#8221;hidden&#8221; name=&#8221;subject&#8221; value=&#8221;Re: &lt;?php echo $subject; ?&gt;&#8221;&gt;</p>
<p>&lt;input type=&#8221;hidden&#8221; name=&#8221;message&#8221; value=&#8221;&lt;?php echo str_replace(&#8220;&lt;br /&gt;&#8221;, &#8220;&#8221;, str_replace(&#8220;\r\n&#8221;, &#8220;\r\n&gt;&gt;&#8221;, $body)) ?&gt;&#8221;&gt;</p>
<p>&lt;h2&gt;&lt;?php echo $subject; ?&gt;&lt;/h2&gt;</p>
<p>&lt;hr noshade size=&#8221;1&#8243; color=&#8221;#20158F&#8221;&gt;</p>
<p>&lt;b&gt;From:&lt;/b&gt; &lt;?php echo str_replace(&#8220;&lt;&#8221;, &#8220;&amp;lt;&#8221;, $from); ?&gt;&lt;br&gt;</p>
<p>&lt;b&gt;Date:&lt;/b&gt; &lt;?php echo $date; ?&gt;&lt;br&gt;</p>
<p>&lt;hr noshade size=&#8221;1&#8243; color=&#8221;#20158F&#8221;&gt;</p>
<p>&lt;?php echo $body; ?&gt;</p>
<p>&lt;hr noshade size=&#8221;1&#8243; color=&#8221;#20158F&#8221;&gt;</p>
<p>&lt;input type=&#8221;submit&#8221; name=&#8221;submit&#8221; value=&#8221;Reply &gt;&gt;&#8221;&gt;</p>
<p>&lt;/form&gt;</p>
<p>As you can see, this form passes the value &#8220;reply&#8221; for the hidden form variable &#8220;what&#8221;. The to, subject and message fields are also passed as hidden form variables, which in turn calls the ShowComposeForm function, which displays a HTML form in exactly the same was as the ShowEmail function.</p>
<p>Although this might sound a little confusing, the ShowComposeForm function is also used to compose a new email. The parameters passed to the function are the values for each text box in the form: if we don&#8217;t pass any parameters then we&#8217;re creating a new form. If we do, then we&#8217;re replying to a message. Here&#8217;s the function signature of ShowComposeForm:</p>
<p><span class="bodyCode">function ShowComposeForm($to = &#8220;&#8221;, $subject = &#8220;&#8221;, $message = &#8220;&#8221;)</span></p>
<p>The ShowComposeForm function includes a hidden form variable, what, which is set to &#8220;send&#8221;, triggering a call to the SendMessage function when the form is submitted.</p>
<p>The function signature of SendMessage looks like this:</p>
<p><span class="bodyCode">SendMessage($to, $subject, $message, $cc, $bcc);</span></p>
<p>SendMessage uses the imap_open function to connect to our mail server. If the connection was successful, it then uses imap_mail to send an email message to the intended recipient, like this:</p>
<p><span class="bodyCode">imap_mail($to, $subject, $message, &#8220;From: $user@$host&#8221;, $cc, $bcc);</span></p>
<p>One last function that I will mention is our DeleteEmails function. This function calls imap_delete as well as imap_explunge to remove email messages from our mailbox based on their ID. The ID&#8217;s for the DeleteEmails function are passed as an array from the form generated by the ShowInbox function:</p>
<p><span class="bodyCode">for($i = 1; $i &lt; $numEmails+1; $i++)</span></p>
<p>&#8230;</p>
<p>&lt;td width=&#8221;5%&#8221;"&gt;</p>
<p>&lt;input type=&#8221;checkbox&#8221; name=&#8221;emails[]&#8221; value=&#8221;&lt;?php echo $i; ?&gt;&#8221;&gt;</p>
<p>&lt;/td&gt;</p>
<p>The imap_delete function accepts two parameters: an IMAP stream object and the ID of the email to delete:</p>
<p><span class="bodyCode">for($i = 0; $i &lt; sizeof($emails); $i++)</span></p>
<p>{</p>
<p>@imap_delete($conn, (int)$emails[$i]);</p>
<p>}</p>
<p>Once we&#8217;ve called imap_delete for each email, we must call the imap_expuge function to actually remove the messages from the server:</p>
<p><span class="bodyCode">@imap_expunge($conn);</span></p>
<p class="pagenavbar">
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/webtechnology.wordpress.com/44/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/webtechnology.wordpress.com/44/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webtechnology.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webtechnology.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webtechnology.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webtechnology.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/webtechnology.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/webtechnology.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/webtechnology.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/webtechnology.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webtechnology.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webtechnology.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webtechnology.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webtechnology.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webtechnology.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webtechnology.wordpress.com/44/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&amp;blog=2417486&amp;post=44&amp;subd=webtechnology&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://webtechnology.wordpress.com/2008/05/26/create-your-own-mail-script-with-php-and-imap/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4682697a6bd2ad125716a325e07284a2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aseefahmed</media:title>
		</media:content>

		<media:content url="http://www.000webhost.com/images/banners/468x60/banner13.gif" medium="image">
			<media:title type="html">Free Website Hosting</media:title>
		</media:content>

		<media:content url="http://www.devarticles.com/images/phpimap_1.gif" medium="image">
			<media:title type="html">Making sure IMAP was configured OK</media:title>
		</media:content>
	</item>
		<item>
		<title>Retrieving data in CakePHP</title>
		<link>http://webtechnology.wordpress.com/2008/04/23/retrieving-data-in-cakephp/</link>
		<comments>http://webtechnology.wordpress.com/2008/04/23/retrieving-data-in-cakephp/#comments</comments>
		<pubDate>Wed, 23 Apr 2008 06:51:24 +0000</pubDate>
		<dc:creator>aseefahmed</dc:creator>
				<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://webtechnology.wordpress.com/?p=41</guid>
		<description><![CDATA[A function to retrieve data in CakePHP is the findAll() model function. It has the following syntax: PLAIN TEXT PHP: findAll($conditions, $fields, $order, $limit, $page, $recursive); string $conditions; array $fields; string $order; int $limit; int $page; int $recursive; For example to retrive all the data from a table you will use following: PLAIN TEXT PHP: [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&amp;blog=2417486&amp;post=41&amp;subd=webtechnology&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div class="post-content">
<p>A function to retrieve data in CakePHP  is the findAll() model function.<br />
It has the following syntax:</p>
<div class="igBar"><span><a href="http://www.bogdan-net.com/archives/16#">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span></p>
<div id="php-1">
<div class="php">
<ol>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;">findAll<span style="color:#006600;font-weight:bold;">(</span><span style="color:#0000ff;">$conditions</span>, <span style="color:#0000ff;">$fields</span>, <span style="color:#0000ff;">$order</span>, <span style="color:#0000ff;">$limit</span>, <span style="color:#0000ff;">$page</span>, <span style="color:#0000ff;">$recursive</span><span style="color:#006600;font-weight:bold;">)</span>;</div>
</li>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;">string <span style="color:#0000ff;">$conditions</span>;</div>
</li>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;"><a href="http://www.php.net/array"><span style="color:#000066;">array</span></a> <span style="color:#0000ff;">$fields</span>;</div>
</li>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;">string <span style="color:#0000ff;">$order</span>;</div>
</li>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;">int <span style="color:#0000ff;">$limit</span>;</div>
</li>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;">int <span style="color:#0000ff;">$page</span>;</div>
</li>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;">int <span style="color:#0000ff;">$recursive</span>;</div>
</li>
</ol>
</div>
</div>
</div>
<p>For example to retrive all the data from a table you will use following:</p>
<div class="igBar"><span><a href="http://www.bogdan-net.com/archives/16#">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span></p>
<div id="php-2">
<div class="php">
<ol>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;"><span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">set</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#0000ff;">$var</span>, <span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">Modelname</span>-&gt;<span style="color:#006600;">findAll</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#006600;font-weight:bold;">)</span><span style="color:#006600;font-weight:bold;">)</span>;</div>
</li>
</ol>
</div>
</div>
</div>
<p>With the “set function” the $var value will be send to the view. In the case above all the fields of the table will be retrieve.</p>
<p>Examples using findAll()</p>
<p>To sort the data by a certain field we can use:</p>
<div class="igBar"><span><a href="http://www.bogdan-net.com/archives/16#">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span></p>
<div id="php-3">
<div class="php">
<ol>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;"><span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">set</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#0000ff;">$var</span>, <span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">Modelname</span>-&gt;<span style="color:#006600;">findAll</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#000000;font-weight:bold;">null</span>, <span style="color:#000000;font-weight:bold;">null</span>, ‘Modelname.field’<span style="color:#006600;font-weight:bold;">)</span><span style="color:#006600;font-weight:bold;">)</span>;</div>
</li>
</ol>
</div>
</div>
</div>
<p>For ascending:</p>
<div class="igBar"><span><a href="http://www.bogdan-net.com/archives/16#">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span></p>
<div id="php-4">
<div class="php">
<ol>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;"><span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">set</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#0000ff;">$var</span>, <span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">Modelname</span>-&gt;<span style="color:#006600;">findAll</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#000000;font-weight:bold;">null</span>, <span style="color:#000000;font-weight:bold;">null</span>, ‘Modelname.field ASC’<span style="color:#006600;font-weight:bold;">)</span><span style="color:#006600;font-weight:bold;">)</span>;</div>
</li>
</ol>
</div>
</div>
</div>
<p>Descending:</p>
<div class="igBar"><span><a href="http://www.bogdan-net.com/archives/16#">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span></p>
<div id="php-5">
<div class="php">
<ol>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;"><span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">set</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#0000ff;">$var</span>, <span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">Modelname</span>-&gt;<span style="color:#006600;">findAll</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#000000;font-weight:bold;">null</span>, <span style="color:#000000;font-weight:bold;">null</span>, ‘Modelname.field DESC’<span style="color:#006600;font-weight:bold;">)</span><span style="color:#006600;font-weight:bold;">)</span>;</div>
</li>
</ol>
</div>
</div>
</div>
<p>Only first 10 results:</p>
<div class="igBar"><span><a href="http://www.bogdan-net.com/archives/16#">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span></p>
<div id="php-6">
<div class="php">
<ol>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;"><span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">set</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#0000ff;">$var</span>, <span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">Modelname</span>-&gt;<span style="color:#006600;">findAll</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#000000;font-weight:bold;">null</span>, <span style="color:#000000;font-weight:bold;">null</span>, ‘Modelname.field DESC’, <span style="color:#800000;">10</span><span style="color:#006600;font-weight:bold;">)</span><span style="color:#006600;font-weight:bold;">)</span>;</div>
</li>
</ol>
</div>
</div>
</div>
<p>The second results page:</p>
<div class="igBar"><span><a href="http://www.bogdan-net.com/archives/16#">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span></p>
<div id="php-7">
<div class="php">
<ol>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;"><span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">set</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#0000ff;">$var</span>, <span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">Modelname</span>-&gt;<span style="color:#006600;">findAll</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#000000;font-weight:bold;">null</span>, <span style="color:#000000;font-weight:bold;">null</span>, ‘Modelname.field DESC’, <span style="color:#800000;">10</span>, <span style="color:#800000;">2</span><span style="color:#006600;font-weight:bold;">)</span><span style="color:#006600;font-weight:bold;">)</span>;</div>
</li>
</ol>
</div>
</div>
</div>
<p>Putting some conditions in the query:</p>
<div class="igBar"><span><a href="http://www.bogdan-net.com/archives/16#">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span></p>
<div id="php-8">
<div class="php">
<ol>
<li>
<div style="font-family:'Courier New',Courier,monospace;font-weight:normal;"><span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">set</span><span style="color:#006600;font-weight:bold;">(</span><span style="color:#0000ff;">$var</span>, <span style="color:#0000ff;">$this</span>-&gt;<span style="color:#006600;">Modelname</span>-&gt;<span style="color:#006600;">findAll</span><span style="color:#006600;font-weight:bold;">(</span>‘Modelname.id&gt; <span style="color:#800000;">1</span>’, <span style="color:#000000;font-weight:bold;">null</span>, ‘Modelname.field DESC’, <span style="color:#800000;">10</span> <span style="color:#006600;font-weight:bold;">)</span><span style="color:#006600;font-weight:bold;">)</span>;</div>
</li>
</ol>
</div>
</div>
</div>
</div>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/webtechnology.wordpress.com/41/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/webtechnology.wordpress.com/41/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webtechnology.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webtechnology.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webtechnology.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webtechnology.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/webtechnology.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/webtechnology.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/webtechnology.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/webtechnology.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webtechnology.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webtechnology.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webtechnology.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webtechnology.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webtechnology.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webtechnology.wordpress.com/41/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&amp;blog=2417486&amp;post=41&amp;subd=webtechnology&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://webtechnology.wordpress.com/2008/04/23/retrieving-data-in-cakephp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4682697a6bd2ad125716a325e07284a2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aseefahmed</media:title>
		</media:content>
	</item>
		<item>
		<title>Transaction behavior</title>
		<link>http://webtechnology.wordpress.com/2008/04/08/transaction-behavior/</link>
		<comments>http://webtechnology.wordpress.com/2008/04/08/transaction-behavior/#comments</comments>
		<pubDate>Tue, 08 Apr 2008 10:44:59 +0000</pubDate>
		<dc:creator>aseefahmed</dc:creator>
				<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://webtechnology.wordpress.com/?p=40</guid>
		<description><![CDATA[Sample model &#8211; note the $actsAs To change default settings do: Download code var $actsAs = array('transaction' =&#62; array('keyName' =&#62; 'value') ); Model Class: Download code &#60;?php class Order extends AppModel { var $name = 'Order'; var $actsAs = array('transaction'); var $hasMany = array('OrderDetail'); ?&#62; Controller Class: Download code &#60;?php class OrderController extends AppController { [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&amp;blog=2417486&amp;post=40&amp;subd=webtechnology&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Sample model &#8211; note the $actsAs</p>
<p>To change default settings do:<br />
<a href="http://bakery.cakephp.org/articles/download_code/228/block/4">Download code</a><code><span style="color:#000000;"><br />
var $actsAs = array('transaction' =&gt; array('keyName' =&gt; 'value') );<br />
</span> </code></p>
<h4><strong>Model Class:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/228/block/1">Download code</a><code><span style="color:#000000;"> <span style="color:#0000bb;">&lt;?php<br />
</span><span style="color:#007700;">class </span><span style="color:#0000bb;">Order </span><span style="color:#007700;">extends </span><span style="color:#0000bb;">AppModel<br />
</span><span style="color:#007700;">{<br />
var </span><span style="color:#0000bb;">$name </span><span style="color:#007700;">= </span><span style="color:#dd0000;">'Order'</span><span style="color:#007700;">;<br />
var </span><span style="color:#0000bb;">$actsAs </span><span style="color:#007700;">= array(</span><span style="color:#dd0000;">'transaction'</span><span style="color:#007700;">);<br />
var </span><span style="color:#0000bb;">$hasMany </span><span style="color:#007700;">= array(</span><span style="color:#dd0000;">'OrderDetail'</span><span style="color:#007700;">);<br />
</span><span style="color:#0000bb;">?&gt;</span> </span> </code></p>
<h4><strong>Controller Class:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/228/block/3">Download code</a><code><span style="color:#000000;"> <span style="color:#0000bb;">&lt;?php<br />
</span><span style="color:#007700;">class </span><span style="color:#0000bb;">OrderController </span><span style="color:#007700;">extends </span><span style="color:#0000bb;">AppController </span><span style="color:#007700;">{<br />
var </span><span style="color:#0000bb;">$name </span><span style="color:#007700;">= </span><span style="color:#dd0000;">'Order'</span><span style="color:#007700;">;<br />
var </span><span style="color:#0000bb;">$uses </span><span style="color:#007700;">= array(</span><span style="color:#dd0000;">'Order'</span><span style="color:#007700;">);</span></span></code></p>
<p>function <span style="color:#0000bb;">checkout</span><span style="color:#007700;">() {<br />
if(!empty(</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">data</span><span style="color:#007700;">)) {<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">Order</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">begin</span><span style="color:#007700;">(); </span><span style="color:#ff8000;">// Start our transaction<br />
</span><span style="color:#007700;">if( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">Order</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">save</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">data </span><span style="color:#007700;">) ) {<br />
if( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">Order</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">OrderDetail</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">save</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">data </span><span style="color:#007700;">) ) {<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">Order</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">commit</span><span style="color:#007700;">(); </span><span style="color:#ff8000;">// Persist the data<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">redirect</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;/shop/order/thanks&#8217;</span><span style="color:#007700;">);<br />
exit;<br />
} else {<br />
</span><span style="color:#ff8000;">// Couldnt save the products to the order<br />
// since the products to the order couldnt be stored, dont store anything at all<br />
// rollback all changes to the database sine &#8216;begin&#8217; was called<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">Order</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">rollback</span><span style="color:#007700;">();<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">redirect</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;/shop/order/error&#8217;</span><span style="color:#007700;">);<br />
exit;<br />
}<br />
} else {<br />
</span><span style="color:#ff8000;">// Didnt save<br />
</span><span style="color:#007700;">}<br />
}<br />
}<br />
}<br />
</span><span style="color:#0000bb;">?&gt;</span></p>
<h4><strong>Model Class:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/228/block/2">Download code</a><code><span style="color:#000000;"> <span style="color:#0000bb;">&lt;?php<br />
</span><span style="color:#ff8000;">/**<br />
* Transaction support for models in 1.2<br />
*<br />
* @version 0.2<br />
* @author Christian 'Jippi' Winther<br />
*/<br />
</span><span style="color:#007700;">class </span><span style="color:#0000bb;">TransactionBehavior </span><span style="color:#007700;">extends </span><span style="color:#0000bb;">ModelBehavior </span><span style="color:#007700;">{<br />
</span><span style="color:#ff8000;">/**<br />
* Settings: (false will disable them)<br />
*<br />
* - autocommit_on_start [ false | 0 | 1 ]<br />
*      Disable auto-commit on start?<br />
*<br />
* - autocommit_after_commit [ false | 0 | 1 ]<br />
*      Set auto-commit after successfull commit<br />
*<br />
* - autocommit_after_rollback [ false | 0 | 1 ]<br />
*      Set auto-commit after successfull rollback<br />
*<br />
* - throw_error [ false | true ]<br />
*      Should we call user_error if something is wrong?<br />
*<br />
* - error_level [ E_USER_* constants ]<br />
*      What level should the user_error use?<br />
*<br />
* - configure_key [ string ]<br />
*       What key should the transaction state be stored as<br />
*<br />
* @version 0.1<br />
* @since   0.1<br />
* @access  private<br />
* @var     array<br />
*/<br />
</span><span style="color:#007700;">var </span><span style="color:#0000bb;">$settings </span><span style="color:#007700;">= array(<br />
</span><span style="color:#dd0000;">'autocommit_on_start' </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">false</span><span style="color:#007700;">,<br />
</span><span style="color:#dd0000;">'autocommit_after_commit' </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">1</span><span style="color:#007700;">,<br />
</span><span style="color:#dd0000;">'autocommit_after_rollback' </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">1</span><span style="color:#007700;">,<br />
</span><span style="color:#dd0000;">'throw_error' </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">false</span><span style="color:#007700;">,<br />
</span><span style="color:#dd0000;">'error_level' </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">E_USER_NOTICE</span><span style="color:#007700;">,<br />
</span><span style="color:#dd0000;">'configure_key' </span><span style="color:#007700;">=&gt; </span><span style="color:#dd0000;">'inTransaction'<br />
</span><span style="color:#007700;">);</span></span></code></p>
<p><span style="color:#ff8000;">/**<br />
* List of legal &#8216;locks&#8217;<br />
*<br />
* @version 0.1<br />
* @since   0.1<br />
* @access  private<br />
* @var     array<br />
*/<br />
</span><span style="color:#007700;">var </span><span style="color:#0000bb;">$validLocks </span><span style="color:#007700;">= array(</span><span style="color:#dd0000;">&#8216;read&#8217;</span><span style="color:#007700;">,</span><span style="color:#dd0000;">&#8216;write&#8217;</span><span style="color:#007700;">);</span></p>
<p><span style="color:#ff8000;">/**<br />
* Startup hook from the model<br />
*<br />
* @version 0.1<br />
* @since   0.1<br />
* @access  public<br />
* @param   AppModel $model<br />
* @param   array $config<br />
*/<br />
</span><span style="color:#007700;">function </span><span style="color:#0000bb;">setup</span><span style="color:#007700;">(&amp;</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$config </span><span style="color:#007700;">= array()) {<br />
</span><span style="color:#ff8000;">/**<br />
* Check if there has been set a transaction state in another model<br />
* Configure::read returns null if no value is found, so its safe to<br />
* set the default value for our transaction state ( false )<br />
*/<br />
</span><span style="color:#007700;">if( </span><span style="color:#0000bb;">is_null</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_getTransactionState</span><span style="color:#007700;">() ) ) {<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_setTransactionState</span><span style="color:#007700;">( </span><span style="color:#0000bb;">false </span><span style="color:#007700;">);<br />
}</span></p>
<p><span style="color:#ff8000;">/**<br />
* If there was given any config settings to this behavior, merge them<br />
* with our default settings array<br />
*/<br />
</span><span style="color:#007700;">if( !</span><span style="color:#0000bb;">is_null</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$config </span><span style="color:#007700;">) &amp;&amp; </span><span style="color:#0000bb;">is_array</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$config </span><span style="color:#007700;">) ) {<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">settings </span><span style="color:#007700;">= </span><span style="color:#0000bb;">array_merge</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">settings</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$config </span><span style="color:#007700;">);<br />
}</span></p>
<p><span style="color:#ff8000;">/**<br />
* If we wish to start an transaction at load time do so<br />
*/<br />
</span><span style="color:#007700;">if( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">settings</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'autocommit_on_start'</span><span style="color:#007700;">] !== </span><span style="color:#0000bb;">false </span><span style="color:#007700;">) {<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">begin</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$model </span><span style="color:#007700;">);<br />
}<br />
}</span></p>
<p><span style="color:#ff8000;">/**<br />
* Begin a database transaction<br />
*<br />
* @version 0.1<br />
* @since   0.1<br />
* @access  public<br />
* @param   AppModel $model<br />
* @param   int $autocmmit<br />
* @return  boolean<br />
*/<br />
</span><span style="color:#007700;">function </span><span style="color:#0000bb;">begin</span><span style="color:#007700;">( &amp;</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$args </span><span style="color:#007700;">= array()) {<br />
if( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_getTransactionState</span><span style="color:#007700;">() === </span><span style="color:#0000bb;">false </span><span style="color:#007700;">) {<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_setTransactionState</span><span style="color:#007700;">( </span><span style="color:#0000bb;">true </span><span style="color:#007700;">);<br />
</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">query</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;SET autocommit=0&#8242;</span><span style="color:#007700;">);<br />
</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">query</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;begin&#8217;</span><span style="color:#007700;">);<br />
return </span><span style="color:#0000bb;">true</span><span style="color:#007700;">;<br />
}<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_error</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;Transaction has already started&#8217;</span><span style="color:#007700;">);<br />
return </span><span style="color:#0000bb;">false</span><span style="color:#007700;">;<br />
}</span></p>
<p><span style="color:#ff8000;">/**<br />
* Commits a database transaction<br />
*<br />
* @version 0.1<br />
* @since   0.1<br />
* @access  public<br />
* @param   AppModel $model<br />
* @param   integer $autocommit<br />
* @return  boolean<br />
*/<br />
</span><span style="color:#007700;">function </span><span style="color:#0000bb;">commit</span><span style="color:#007700;">( &amp;</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$args </span><span style="color:#007700;">= array() ) {<br />
if( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_getTransactionState</span><span style="color:#007700;">() === </span><span style="color:#0000bb;">true </span><span style="color:#007700;">) {<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_setTransactionState</span><span style="color:#007700;">( </span><span style="color:#0000bb;">false </span><span style="color:#007700;">);</span></p>
<p><span style="color:#0000bb;">$autoCommit </span><span style="color:#007700;">= empty(</span><span style="color:#0000bb;">$args</span><span style="color:#007700;">[</span><span style="color:#0000bb;">0</span><span style="color:#007700;">]) ? </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">settings</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'autocommit_after_commit'</span><span style="color:#007700;">] : </span><span style="color:#0000bb;">$args</span><span style="color:#007700;">[</span><span style="color:#0000bb;">0</span><span style="color:#007700;">];<br />
</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">query</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;commit&#8217;</span><span style="color:#007700;">);<br />
if( </span><span style="color:#0000bb;">$autoCommit </span><span style="color:#007700;">!== </span><span style="color:#0000bb;">false </span><span style="color:#007700;">) {<br />
</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">query</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;SET autocommit=&#8217;</span><span style="color:#007700;">. </span><span style="color:#0000bb;">$autoCommit</span><span style="color:#007700;">);<br />
}<br />
return </span><span style="color:#0000bb;">true</span><span style="color:#007700;">;<br />
}<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_error</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;No transaction active&#8217;</span><span style="color:#007700;">);<br />
return </span><span style="color:#0000bb;">false</span><span style="color:#007700;">;<br />
}</span></p>
<p><span style="color:#ff8000;">/**<br />
* Rollback a transaction<br />
*<br />
* @version 0.1<br />
* @since   0.1<br />
* @access  public<br />
* @param   AppModel $model<br />
* @param   integer $autocommit<br />
*/<br />
</span><span style="color:#007700;">function </span><span style="color:#0000bb;">rollback</span><span style="color:#007700;">( &amp;</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$args </span><span style="color:#007700;">) {<br />
if( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_getTransactionState </span><span style="color:#007700;">=== </span><span style="color:#0000bb;">true </span><span style="color:#007700;">) {<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_setTransactionState</span><span style="color:#007700;">( </span><span style="color:#0000bb;">false </span><span style="color:#007700;">);<br />
</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">query</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;rollback&#8217;</span><span style="color:#007700;">);<br />
</span><span style="color:#0000bb;">$autoCommit </span><span style="color:#007700;">= empty(</span><span style="color:#0000bb;">$args</span><span style="color:#007700;">[</span><span style="color:#0000bb;">0</span><span style="color:#007700;">]) ? </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">settings</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'autocommit_after_rollback'</span><span style="color:#007700;">] : </span><span style="color:#0000bb;">$args</span><span style="color:#007700;">[</span><span style="color:#0000bb;">0</span><span style="color:#007700;">];<br />
if( </span><span style="color:#0000bb;">$autoCommit </span><span style="color:#007700;">!== </span><span style="color:#0000bb;">false </span><span style="color:#007700;">) {<br />
</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">query</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;SET autocommit=&#8217;</span><span style="color:#007700;">. </span><span style="color:#0000bb;">$autoCommit</span><span style="color:#007700;">);<br />
}<br />
return </span><span style="color:#0000bb;">true</span><span style="color:#007700;">;<br />
}<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_error</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;No transaction active&#8217;</span><span style="color:#007700;">);<br />
return </span><span style="color:#0000bb;">false</span><span style="color:#007700;">;<br />
}</span></p>
<p><span style="color:#ff8000;">/**<br />
* Locks the model table for either READ or WRITE<br />
*<br />
* &#8211; Arg[0] is either &#8216;READ&#8217; or &#8216;WRITE&#8217;<br />
* &#8211; Arg[1]:<br />
*     If null     : Its going to lock the model table<br />
*     If string   : Locking the given table<br />
*     If array    : Going to lock all tables specified<br />
*                   NOTE: You have to specify model table aswell!<br />
*<br />
* @version 0.2<br />
* @since   0.1<br />
* @access  public<br />
* @param   AppModel $model<br />
* @param   array $args<br />
*/<br />
</span><span style="color:#007700;">function </span><span style="color:#0000bb;">lockTable</span><span style="color:#007700;">( &amp;</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$args </span><span style="color:#007700;">= array( ) ) {<br />
</span><span style="color:#0000bb;">$type </span><span style="color:#007700;">= </span><span style="color:#0000bb;">strtolower</span><span style="color:#007700;">( empty( </span><span style="color:#0000bb;">$args</span><span style="color:#007700;">[</span><span style="color:#0000bb;">0</span><span style="color:#007700;">] ) ? </span><span style="color:#0000bb;">null </span><span style="color:#007700;">: </span><span style="color:#0000bb;">$args</span><span style="color:#007700;">[</span><span style="color:#0000bb;">0</span><span style="color:#007700;">] );<br />
</span><span style="color:#0000bb;">$table </span><span style="color:#007700;">= </span><span style="color:#0000bb;">strtolower</span><span style="color:#007700;">( empty( </span><span style="color:#0000bb;">$args</span><span style="color:#007700;">[</span><span style="color:#0000bb;">1</span><span style="color:#007700;">] ) ? </span><span style="color:#0000bb;">$model</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">table </span><span style="color:#007700;">: </span><span style="color:#0000bb;">$args</span><span style="color:#007700;">[</span><span style="color:#0000bb;">1</span><span style="color:#007700;">] );<br />
if( </span><span style="color:#0000bb;">is_null</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$type </span><span style="color:#007700;">) ) {<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_error</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;Missing parameter for lockTable(), either READ or WRITE&#8217;</span><span style="color:#007700;">);<br />
return </span><span style="color:#0000bb;">false</span><span style="color:#007700;">;<br />
}<br />
if( </span><span style="color:#0000bb;">array_search</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$type</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">validLocks </span><span style="color:#007700;">) !== </span><span style="color:#0000bb;">false </span><span style="color:#007700;">) {<br />
if(</span><span style="color:#0000bb;">is_array</span><span style="color:#007700;">(</span><span style="color:#0000bb;">$table</span><span style="color:#007700;">)) {<br />
</span><span style="color:#0000bb;">$res </span><span style="color:#007700;">= array();<br />
foreach ( </span><span style="color:#0000bb;">$table </span><span style="color:#007700;">AS </span><span style="color:#0000bb;">$tableName </span><span style="color:#007700;">) {<br />
</span><span style="color:#0000bb;">$res</span><span style="color:#007700;">[ </span><span style="color:#0000bb;">$tableName </span><span style="color:#007700;">] = </span><span style="color:#0000bb;">$model</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">query</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;LOCK TABLE &#8216;</span><span style="color:#007700;">.</span><span style="color:#0000bb;">$table</span><span style="color:#007700;">.</span><span style="color:#dd0000;">&#8216; &#8216;</span><span style="color:#007700;">. </span><span style="color:#0000bb;">strtoupper</span><span style="color:#007700;">(</span><span style="color:#0000bb;">$type</span><span style="color:#007700;">));<br />
}<br />
return </span><span style="color:#0000bb;">$res</span><span style="color:#007700;">;<br />
} else {<br />
</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">query</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;LOCK TABLE &#8216;</span><span style="color:#007700;">.</span><span style="color:#0000bb;">$table</span><span style="color:#007700;">.</span><span style="color:#dd0000;">&#8216; &#8216;</span><span style="color:#007700;">. </span><span style="color:#0000bb;">strtoupper</span><span style="color:#007700;">(</span><span style="color:#0000bb;">$type</span><span style="color:#007700;">));<br />
return </span><span style="color:#0000bb;">true</span><span style="color:#007700;">;<br />
}<br />
}<br />
return </span><span style="color:#0000bb;">false</span><span style="color:#007700;">;<br />
}</span></p>
<p><span style="color:#ff8000;">/**<br />
* Remove all locks made on tables<br />
*<br />
* @version 0.1<br />
* @since   0.1<br />
* @access  public<br />
* @param   AppModel $model<br />
* @param   array $args<br />
*/<br />
</span><span style="color:#007700;">function </span><span style="color:#0000bb;">unlockTable</span><span style="color:#007700;">( &amp;</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$args </span><span style="color:#007700;">= array() ) {<br />
</span><span style="color:#0000bb;">$model</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">query</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;UNLOCK TABLES&#8217;</span><span style="color:#007700;">);<br />
}</span></p>
<p><span style="color:#ff8000;">/**<br />
* Show an error to screen (When debug &gt; 0 )<br />
*<br />
* @version 0.1<br />
* @since 0.1<br />
* @access private<br />
* @param string $string<br />
*/<br />
</span><span style="color:#007700;">function </span><span style="color:#0000bb;">_error</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$string </span><span style="color:#007700;">) {<br />
if( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">settings</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'throw_error'</span><span style="color:#007700;">] ) {<br />
</span><span style="color:#0000bb;">trigger_error</span><span style="color:#007700;">(</span><span style="color:#0000bb;">$string</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">settings</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'error_level'</span><span style="color:#007700;">] );<br />
}<br />
}</span></p>
<p><span style="color:#ff8000;">/**<br />
* Get the current transaction state<br />
*<br />
* @version 0.1<br />
* @since 0.1<br />
* @access private<br />
* @return boolean<br />
*/<br />
</span><span style="color:#007700;">function </span><span style="color:#0000bb;">_getTransactionState</span><span style="color:#007700;">() {<br />
return </span><span style="color:#0000bb;">Configure</span><span style="color:#007700;">::</span><span style="color:#0000bb;">read</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">settings</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'configure_key'</span><span style="color:#007700;">] );<br />
}</span></p>
<p><span style="color:#ff8000;">/**<br />
* Write a new transaction state<br />
*<br />
* @version 0.1<br />
* @since 0.1<br />
* @access private<br />
* @param boolean $value<br />
*/<br />
</span><span style="color:#007700;">function </span><span style="color:#0000bb;">_setTransactionState</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$value </span><span style="color:#007700;">) {<br />
if( </span><span style="color:#0000bb;">is_bool</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$value </span><span style="color:#007700;">) ) {<br />
</span><span style="color:#0000bb;">Configure</span><span style="color:#007700;">::</span><span style="color:#0000bb;">write</span><span style="color:#007700;">( </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">settings</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'configure_key'</span><span style="color:#007700;">], </span><span style="color:#0000bb;">$value </span><span style="color:#007700;">);<br />
}<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">_error</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;Not valid transaction state&#8217;</span><span style="color:#007700;">);<br />
}<br />
}<br />
</span><span style="color:#0000bb;">?&gt;</span><!-- comments --></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/webtechnology.wordpress.com/40/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/webtechnology.wordpress.com/40/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webtechnology.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webtechnology.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webtechnology.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webtechnology.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/webtechnology.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/webtechnology.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/webtechnology.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/webtechnology.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webtechnology.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webtechnology.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webtechnology.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webtechnology.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webtechnology.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webtechnology.wordpress.com/40/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&amp;blog=2417486&amp;post=40&amp;subd=webtechnology&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://webtechnology.wordpress.com/2008/04/08/transaction-behavior/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4682697a6bd2ad125716a325e07284a2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aseefahmed</media:title>
		</media:content>
	</item>
		<item>
		<title>Creating Reusable Elements with requestAction</title>
		<link>http://webtechnology.wordpress.com/2008/04/08/creating-reusable-elements-with-requestaction/</link>
		<comments>http://webtechnology.wordpress.com/2008/04/08/creating-reusable-elements-with-requestaction/#comments</comments>
		<pubDate>Tue, 08 Apr 2008 04:12:11 +0000</pubDate>
		<dc:creator>aseefahmed</dc:creator>
				<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://webtechnology.wordpress.com/?p=39</guid>
		<description><![CDATA[Creating reusable elements with requestAction is very simple. At the end, we can even cache the element using the new feature in 1.2. Start of with a simple controller. Controller Class: Download code &#60;?php class PostsController extends AppController { var $name = 'Posts'; function index() { $posts = $this-&#62;Post-&#62;findAll(); if(isset($this-&#62;params['requested'])) { return $posts; } $this-&#62;set('posts', $posts); } [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&amp;blog=2417486&amp;post=39&amp;subd=webtechnology&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Creating reusable elements with requestAction is very simple. At the end, we can even cache the element using the new feature in 1.2.</p>
<p>Start of with a simple controller.</p>
<h4><strong>Controller Class:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/342/block/5">Download code</a><code><span style="color:#000000;"> <span style="color:#0000bb;">&lt;?php<br />
</span><span style="color:#007700;">class </span><span style="color:#0000bb;">PostsController </span><span style="color:#007700;">extends </span><span style="color:#0000bb;">AppController </span><span style="color:#007700;">{<br />
var </span><span style="color:#0000bb;">$name </span><span style="color:#007700;">= </span><span style="color:#dd0000;">'Posts'</span><span style="color:#007700;">;</p>
<p>function </span><span style="color:#0000bb;">index</span><span style="color:#007700;">() {<br />
</span><span style="color:#0000bb;">$posts </span><span style="color:#007700;">= </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">Post</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">findAll</span><span style="color:#007700;">();<br />
if(isset(</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">params</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'requested'</span><span style="color:#007700;">])) {<br />
return </span><span style="color:#0000bb;">$posts</span><span style="color:#007700;">;<br />
}<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">set</span><span style="color:#007700;">(</span><span style="color:#dd0000;">'posts'</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$posts</span><span style="color:#007700;">);<br />
}<br />
}<br />
</span><span style="color:#0000bb;">?&gt;</span> </span> </code><br />
So, we created the Posts controller and we gave it an index action. Then we found all the posts, then if requestAction is used it will return the posts, otherwise it will set the posts for the view.</p>
<p>Now we can create a reusable element that will use requestAction. We create a file in /app/elements/posts.thtml, then add the code below.</p>
<h4><strong>View Template:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/342/block/1">Download code</a><code><span style="color:#000000;"><br />
$posts = $this-&gt;requestAction('posts/index');<br />
foreach($posts as $post):<br />
echo $post['Post']['title'];<br />
endforeach;<br />
</span> </code></p>
<p>Now we have the element we can include it in the layout, another element or a view template.</p>
<h4><strong>View Template:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/342/block/2">Download code</a><code><span style="color:#000000;"><br />
<span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">renderElement</span><span style="color:#007700;">(</span><span style="color:#dd0000;">'posts'</span><span style="color:#007700;">);</span><span style="color:#0000bb;">?&gt;<br />
</span> </span> </code></p>
<p>The code above is for 1.1. If you want to get into the goodness of Cake 1.2 you can look at the updated code below.</p>
<h4><strong>Controller Class:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/342/block/6">Download code</a><code><span style="color:#000000;"> <span style="color:#0000bb;">&lt;?php<br />
</span><span style="color:#007700;">class </span><span style="color:#0000bb;">PostsController </span><span style="color:#007700;">extends </span><span style="color:#0000bb;">AppController </span><span style="color:#007700;">{<br />
var </span><span style="color:#0000bb;">$name </span><span style="color:#007700;">= </span><span style="color:#dd0000;">'Posts'</span><span style="color:#007700;">;</p>
<p>function </span><span style="color:#0000bb;">index</span><span style="color:#007700;">() {<br />
</span><span style="color:#0000bb;">$posts </span><span style="color:#007700;">= </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">paginate</span><span style="color:#007700;">();<br />
if(isset(</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">params</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'requested'</span><span style="color:#007700;">])) {<br />
return </span><span style="color:#0000bb;">$posts</span><span style="color:#007700;">;<br />
}<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">set</span><span style="color:#007700;">(</span><span style="color:#dd0000;">'posts'</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$posts</span><span style="color:#007700;">);<br />
}<br />
}<br />
</span><span style="color:#0000bb;">?&gt;</span> </span> </code><br />
Notice we changed the index action to use paginate instead.<br />
In the view we can use paginate params to control what is returned.</p>
<h4><strong>View Template:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/342/block/3">Download code</a><code><span style="color:#000000;"><br />
$posts = $this-&gt;requestAction('posts/index/sort:created/direction:desc/limit:10');<br />
foreach($posts as $post):<br />
echo $post['Post']['title'];<br />
endforeach;<br />
</span> </code><br />
Now we are only grabbing the latest 10 posts. Whoa, we just created a latest posts element without having to change our index action or create another action.</p>
<p>Lets call this one latest posts and cache it for an hour.<br />
create the file /app/elements/latest_posts.ctp.</p>
<h4><strong>View Template:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/342/block/4">Download code</a><code><span style="color:#000000;"><br />
<span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">element</span><span style="color:#007700;">(</span><span style="color:#dd0000;">'latest_posts'</span><span style="color:#007700;">, array(</span><span style="color:#dd0000;">'cache'</span><span style="color:#007700;">=&gt;</span><span style="color:#dd0000;">'+1 hour'</span><span style="color:#007700;">);</span><span style="color:#0000bb;">?&gt;<br />
</span> </span> </code><br />
We can still use this element in any other view file.</p>
<p>There are other ways to use requestAction, but if you are looking to create reusable elements for your site, then this is the way to go.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/webtechnology.wordpress.com/39/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/webtechnology.wordpress.com/39/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webtechnology.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webtechnology.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webtechnology.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webtechnology.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/webtechnology.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/webtechnology.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/webtechnology.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/webtechnology.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webtechnology.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webtechnology.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webtechnology.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webtechnology.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webtechnology.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webtechnology.wordpress.com/39/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&amp;blog=2417486&amp;post=39&amp;subd=webtechnology&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://webtechnology.wordpress.com/2008/04/08/creating-reusable-elements-with-requestaction/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4682697a6bd2ad125716a325e07284a2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aseefahmed</media:title>
		</media:content>
	</item>
		<item>
		<title>Multiple rules of validation per field in CakePHP 1.2</title>
		<link>http://webtechnology.wordpress.com/2008/04/07/multiple-rules-of-validation-per-field-in-cakephp-12/</link>
		<comments>http://webtechnology.wordpress.com/2008/04/07/multiple-rules-of-validation-per-field-in-cakephp-12/#comments</comments>
		<pubDate>Mon, 07 Apr 2008 11:46:46 +0000</pubDate>
		<dc:creator>aseefahmed</dc:creator>
				<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://webtechnology.wordpress.com/?p=38</guid>
		<description><![CDATA[On its 1.1 release, CakePHP only allowed us to define one rule of validation per field. If we needed to specify more than one rule, we either had to use some handy extensions to CakePHP that are available, or achieve multiple validation by using the callback method beforeValidate() in our models. CakePHP 1.2 brings us [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&amp;blog=2417486&amp;post=38&amp;subd=webtechnology&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>On its 1.1 release, CakePHP only allowed us to define one rule of validation per field. If we needed to specify more than one rule, we either had to use some handy extensions to CakePHP that are available, or achieve multiple validation by using the callback method beforeValidate() in our models. CakePHP 1.2 brings us an improved technique to specify multiple rules per field, and best of all, it&#8217;s backwards compatible, so our existing rules will continue to work.</p>
<p>What better way to start that with an example, specifically the typical Article CRUD example. We start by creating a simple controller that provide us with an add form:</p>
<h4><strong>Controller Class:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/369/block/9">Download code</a><code><span style="color:#000000;"> <span style="color:#0000bb;">&lt;?php<br />
</span><span style="color:#007700;">class </span><span style="color:#0000bb;">ArticlesController </span><span style="color:#007700;">extends </span><span style="color:#0000bb;">AppController </span><span style="color:#007700;">{<br />
var </span><span style="color:#0000bb;">$name </span><span style="color:#007700;">= </span><span style="color:#dd0000;">'Articles'</span><span style="color:#007700;">;<br />
var </span><span style="color:#0000bb;">$helpers </span><span style="color:#007700;">= array(</span><span style="color:#dd0000;">'Form'</span><span style="color:#007700;">);</span></span></code></p>
<p>function <span style="color:#0000bb;">add</span><span style="color:#007700;">() {<br />
if (!empty(</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">data</span><span style="color:#007700;">)) {<br />
</span><span style="color:#ff8000;">// We don&#8217;t do any real saving, we just validate the model</span></p>
<p><span style="color:#007700;">if (</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">Article</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">create</span><span style="color:#007700;">(</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">data</span><span style="color:#007700;">) &amp;&amp; </span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">Article</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">validates</span><span style="color:#007700;">()) {<br />
</span><span style="color:#0000bb;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">set</span><span style="color:#007700;">(</span><span style="color:#dd0000;">&#8216;valid&#8217;</span><span style="color:#007700;">, </span><span style="color:#0000bb;">true</span><span style="color:#007700;">);<br />
}<br />
}<br />
}<br />
}<br />
</span><span style="color:#0000bb;">?&gt;</span></p>
<p>and its matching articles/add.ctp view:</p>
<h4><strong>View Template:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/369/block/5">Download code</a><code><span style="color:#000000;"><br />
<span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">create</span><span style="color:#007700;">(</span><span style="color:#dd0000;">'Article'</span><span style="color:#007700;">); </span><span style="color:#0000bb;">?&gt;<br />
</span> <span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">input</span><span style="color:#007700;">(</span><span style="color:#dd0000;">'title'</span><span style="color:#007700;">); </span><span style="color:#0000bb;">?&gt;<br />
</span> <span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">input</span><span style="color:#007700;">(</span><span style="color:#dd0000;">'body'</span><span style="color:#007700;">); </span><span style="color:#0000bb;">?&gt;<br />
&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">end</span><span style="color:#007700;">(</span><span style="color:#dd0000;">'Add Article'</span><span style="color:#007700;">); </span><span style="color:#0000bb;">?&gt;<br />
</span> </span> </code></p>
<p>Let&#8217;s take the classic Article model, and add only one validation to some fields:</p>
<h4><strong>Model Class:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/369/block/1">Download code</a><code><span style="color:#000000;"> <span style="color:#0000bb;">&lt;?php<br />
</span><span style="color:#007700;">class </span><span style="color:#0000bb;">Article </span><span style="color:#007700;">extends </span><span style="color:#0000bb;">AppModel </span><span style="color:#007700;">{<br />
var </span><span style="color:#0000bb;">$name </span><span style="color:#007700;">= </span><span style="color:#dd0000;">'Article'</span><span style="color:#007700;">;<br />
var </span><span style="color:#0000bb;">$validate </span><span style="color:#007700;">= array(<br />
</span><span style="color:#dd0000;">'title' </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">VALID_NOT_EMPTY</span><span style="color:#007700;">,<br />
</span><span style="color:#dd0000;">'body' </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">VALID_NOT_EMPTY<br />
</span><span style="color:#007700;">);<br />
}<br />
</span><span style="color:#0000bb;">?&gt;</span> </span> </code></p>
<p>Nothing new here, except that as you can see we&#8217;re not defining the error messages. We are going to do so in the view. So we go back and change the add.ctp view so it now looks like this:</p>
<h4><strong>View Template:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/369/block/6">Download code</a><code><span style="color:#000000;"><br />
<span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">create</span><span style="color:#007700;">(</span><span style="color:#dd0000;">'Article'</span><span style="color:#007700;">); </span><span style="color:#0000bb;">?&gt;<br />
</span> <span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">input</span><span style="color:#007700;">(</span><span style="color:#dd0000;">'title'</span><span style="color:#007700;">, array(</span><span style="color:#dd0000;">'error' </span><span style="color:#007700;">=&gt; </span><span style="color:#dd0000;">'Please specify a valid title'</span><span style="color:#007700;">)); </span><span style="color:#0000bb;">?&gt;<br />
</span> <span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">input</span><span style="color:#007700;">(</span><span style="color:#dd0000;">'body'</span><span style="color:#007700;">, array(</span><span style="color:#dd0000;">'error' </span><span style="color:#007700;">=&gt; </span><span style="color:#dd0000;">'Please specify a valid body'</span><span style="color:#007700;">)); </span><span style="color:#0000bb;">?&gt;<br />
&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">end</span><span style="color:#007700;">(</span><span style="color:#dd0000;">'Add Article'</span><span style="color:#007700;">); </span><span style="color:#0000bb;">?&gt;<br />
</span> </span> </code></p>
<p>If we try the add action and submit the form with both fields empty, we&#8217;ll get our newly defined error messages. What if we also wanted to add another rule of validation for the title field, where we should check that the title is at the most 100 characters long? Let&#8217;s review the model, and now change it to look like this:</p>
<h4><strong>Model Class:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/369/block/2">Download code</a><code><span style="color:#000000;"> <span style="color:#0000bb;">&lt;?php<br />
</span><span style="color:#007700;">class </span><span style="color:#0000bb;">Article </span><span style="color:#007700;">extends </span><span style="color:#0000bb;">AppModel </span><span style="color:#007700;">{<br />
var </span><span style="color:#0000bb;">$name </span><span style="color:#007700;">= </span><span style="color:#dd0000;">'Article'</span><span style="color:#007700;">;<br />
var </span><span style="color:#0000bb;">$validate </span><span style="color:#007700;">= array(<br />
</span><span style="color:#dd0000;">'title' </span><span style="color:#007700;">=&gt; array(<br />
</span><span style="color:#0000bb;">VALID_NOT_EMPTY</span><span style="color:#007700;">,<br />
array(<br />
</span><span style="color:#dd0000;">'rule' </span><span style="color:#007700;">=&gt; array(</span><span style="color:#dd0000;">'maxLength'</span><span style="color:#007700;">, </span><span style="color:#0000bb;">100</span><span style="color:#007700;">)<br />
)<br />
),<br />
</span><span style="color:#dd0000;">'body' </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">VALID_NOT_EMPTY<br />
</span><span style="color:#007700;">);<br />
}<br />
</span><span style="color:#0000bb;">?&gt;</span> </span> </code></p>
<p>We have just converted the &#8216;title&#8217; field to consist of an array of rules. The first rule is a CakePHP provided regular expression that checks for a specified value, and the second rule is a method existing in CakePHP&#8217;s built in Validation class called &#8216;maxLength&#8217;. Since this method takes an extra parameter (the number of maximum characters the field can contain), we add it on an array.</p>
<p>So we have now two conditions that can generate an error for the field title: an empty value, or a value with more than 100 characters. How do we differentiate the error messages on the view? Let&#8217;s change the view so it now looks like:</p>
<h4><strong>View Template:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/369/block/7">Download code</a><code><span style="color:#000000;"><br />
<span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">create</span><span style="color:#007700;">(</span><span style="color:#dd0000;">'Article'</span><span style="color:#007700;">); </span><span style="color:#0000bb;">?&gt;<br />
</span> <span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">input</span><span style="color:#007700;">(</span><span style="color:#dd0000;">'title'</span><span style="color:#007700;">, array(</span><span style="color:#dd0000;">'error' </span><span style="color:#007700;">=&gt; array(<br />
</span><span style="color:#0000bb;">0 </span><span style="color:#007700;">=&gt; </span><span style="color:#dd0000;">'Please specify a valid title'</span><span style="color:#007700;">,<br />
</span><span style="color:#0000bb;">1 </span><span style="color:#007700;">=&gt; </span><span style="color:#dd0000;">'The title must have no more than 100 characters'<br />
</span><span style="color:#007700;">))); </span><span style="color:#0000bb;">?&gt;<br />
</span> <span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">input</span><span style="color:#007700;">(</span><span style="color:#dd0000;">'body'</span><span style="color:#007700;">, array(</span><span style="color:#dd0000;">'error' </span><span style="color:#007700;">=&gt; </span><span style="color:#dd0000;">'Please specify a valid body'</span><span style="color:#007700;">)); </span><span style="color:#0000bb;">?&gt;<br />
&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">end</span><span style="color:#007700;">(</span><span style="color:#dd0000;">'Add Article'</span><span style="color:#007700;">); </span><span style="color:#0000bb;">?&gt;<br />
</span> </span> </code></p>
<p>As you can see we&#8217;re setting an error message per rule. 0 corresponds to the first rule, 1 to the second, and so on. If we wanted more flexibility (such as having the option to change the order of the rules and still have the same error message assignment) and needed more readability, we can then use the string index approach. Change the model so it now looks like:</p>
<h4><strong>Model Class:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/369/block/3">Download code</a><code><span style="color:#000000;"> <span style="color:#0000bb;">&lt;?php<br />
</span><span style="color:#007700;">class </span><span style="color:#0000bb;">Article </span><span style="color:#007700;">extends </span><span style="color:#0000bb;">AppModel </span><span style="color:#007700;">{<br />
var </span><span style="color:#0000bb;">$name </span><span style="color:#007700;">= </span><span style="color:#dd0000;">'Article'</span><span style="color:#007700;">;<br />
var </span><span style="color:#0000bb;">$validate </span><span style="color:#007700;">= array(<br />
</span><span style="color:#dd0000;">'title' </span><span style="color:#007700;">=&gt; array(<br />
</span><span style="color:#dd0000;">'required' </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">VALID_NOT_EMPTY</span><span style="color:#007700;">,<br />
</span><span style="color:#dd0000;">'length' </span><span style="color:#007700;">=&gt; array( </span><span style="color:#dd0000;">'rule' </span><span style="color:#007700;">=&gt; array(</span><span style="color:#dd0000;">'maxLength'</span><span style="color:#007700;">, </span><span style="color:#0000bb;">100</span><span style="color:#007700;">) )<br />
),<br />
</span><span style="color:#dd0000;">'body' </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">VALID_NOT_EMPTY<br />
</span><span style="color:#007700;">);<br />
}<br />
</span><span style="color:#0000bb;">?&gt;</span> </span> </code></p>
<p>and change the view so it now looks like:</p>
<h4><strong>View Template:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/369/block/8">Download code</a><code><span style="color:#000000;"><br />
<span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">create</span><span style="color:#007700;">(</span><span style="color:#dd0000;">'Article'</span><span style="color:#007700;">); </span><span style="color:#0000bb;">?&gt;<br />
</span> <span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">input</span><span style="color:#007700;">(</span><span style="color:#dd0000;">'title'</span><span style="color:#007700;">, array(</span><span style="color:#dd0000;">'error' </span><span style="color:#007700;">=&gt; array(<br />
</span><span style="color:#dd0000;">'required' </span><span style="color:#007700;">=&gt; </span><span style="color:#dd0000;">'Please specify a valid title'</span><span style="color:#007700;">,<br />
</span><span style="color:#dd0000;">'length' </span><span style="color:#007700;">=&gt; </span><span style="color:#dd0000;">'The title must have no more than 100 characters'<br />
</span><span style="color:#007700;">))); </span><span style="color:#0000bb;">?&gt;<br />
</span> <span style="color:#0000bb;">&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">input</span><span style="color:#007700;">(</span><span style="color:#dd0000;">'body'</span><span style="color:#007700;">, array(</span><span style="color:#dd0000;">'error' </span><span style="color:#007700;">=&gt; </span><span style="color:#dd0000;">'Please specify a valid body'</span><span style="color:#007700;">)); </span><span style="color:#0000bb;">?&gt;<br />
&lt;?php </span><span style="color:#007700;">echo </span><span style="color:#0000bb;">$form</span><span style="color:#007700;">-&gt;</span><span style="color:#0000bb;">end</span><span style="color:#007700;">(</span><span style="color:#dd0000;">'Add Article'</span><span style="color:#007700;">); </span><span style="color:#0000bb;">?&gt;<br />
</span> </span> </code></p>
<h3>Custom Validation</h3>
<p>What about custom validation? What if we needed more rules than those provided by CakePHP&#8217;s Validation class? Don&#8217;t sweat, it comes very easy! All you need to do is set up your own validation functions on either your model or your AppModel class (if you wish to share them across your models.) For example, we&#8217;re going to add a new validation rule to allow us to specify a minimum and a maximum length for our title. I know, what&#8217;s the point when we have both minLength and maxLength in CakePHP&#8217;s Validation class? Well, to show how it can be done <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Edit the model and change it so it now looks like this:</p>
<h4><strong>Model Class:</strong></h4>
<p><a href="http://bakery.cakephp.org/articles/download_code/369/block/4">Download code</a><code><span style="color:#000000;"> <span style="color:#0000bb;">&lt;?php<br />
</span><span style="color:#007700;">class </span><span style="color:#0000bb;">Article </span><span style="color:#007700;">extends </span><span style="color:#0000bb;">AppModel </span><span style="color:#007700;">{<br />
var </span><span style="color:#0000bb;">$name </span><span style="color:#007700;">= </span><span style="color:#dd0000;">'Article'</span><span style="color:#007700;">;<br />
var </span><span style="color:#0000bb;">$validate </span><span style="color:#007700;">= array(<br />
</span><span style="color:#dd0000;">'title' </span><span style="color:#007700;">=&gt; array(<br />
</span><span style="color:#dd0000;">'required' </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">VALID_NOT_EMPTY</span><span style="color:#007700;">,<br />
</span><span style="color:#dd0000;">'length' </span><span style="color:#007700;">=&gt; array( </span><span style="color:#dd0000;">'rule' </span><span style="color:#007700;">=&gt; </span><span style="color:#dd0000;">'validateLength'</span><span style="color:#007700;">, </span><span style="color:#dd0000;">'min' </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">5</span><span style="color:#007700;">, </span><span style="color:#dd0000;">'max' </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">100 </span><span style="color:#007700;">)<br />
),<br />
</span><span style="color:#dd0000;">'body' </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">VALID_NOT_EMPTY<br />
</span><span style="color:#007700;">);</span></span></code></p>
<p>function <span style="color:#0000bb;">validateLength</span><span style="color:#007700;">(</span><span style="color:#0000bb;">$value</span><span style="color:#007700;">, </span><span style="color:#0000bb;">$params </span><span style="color:#007700;">= array()) {<br />
</span><span style="color:#0000bb;">$valid </span><span style="color:#007700;">= </span><span style="color:#0000bb;">false</span><span style="color:#007700;">;</span></p>
<p><span style="color:#0000bb;">$params </span><span style="color:#007700;">= </span><span style="color:#0000bb;">am</span><span style="color:#007700;">(array(<br />
</span><span style="color:#dd0000;">&#8216;min&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">null</span><span style="color:#007700;">,<br />
</span><span style="color:#dd0000;">&#8216;max&#8217; </span><span style="color:#007700;">=&gt; </span><span style="color:#0000bb;">null</span><span style="color:#007700;">,<br />
), </span><span style="color:#0000bb;">$params</span><span style="color:#007700;">);</span></p>
<p>if (empty(<span style="color:#0000bb;">$params</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'min'</span><span style="color:#007700;">]) || empty(</span><span style="color:#0000bb;">$params</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'max'</span><span style="color:#007700;">])) {<br />
</span><span style="color:#0000bb;">$valid </span><span style="color:#007700;">= </span><span style="color:#0000bb;">false</span><span style="color:#007700;">;<br />
} else if (</span><span style="color:#0000bb;">strlen</span><span style="color:#007700;">(</span><span style="color:#0000bb;">$value</span><span style="color:#007700;">) &gt;= </span><span style="color:#0000bb;">$params</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'min'</span><span style="color:#007700;">] &amp;&amp; </span><span style="color:#0000bb;">strlen</span><span style="color:#007700;">(</span><span style="color:#0000bb;">$value</span><span style="color:#007700;">) &lt;= </span><span style="color:#0000bb;">$params</span><span style="color:#007700;">[</span><span style="color:#dd0000;">'max'</span><span style="color:#007700;">]) {<br />
</span><span style="color:#0000bb;">$valid </span><span style="color:#007700;">= </span><span style="color:#0000bb;">true</span><span style="color:#007700;">;<br />
}</span></p>
<p>return <span style="color:#0000bb;">$valid</span><span style="color:#007700;">;<br />
}<br />
}<br />
</span><span style="color:#0000bb;">?&gt;</span></p>
<p>A custom validation function takes one mandatory first parameter: the value to validate, and must return a boolean value of true when the value validates, or false when it doesn&#8217;t. Extra parameters will be sent to the validation function as an array through its second parameter, and the values in the array are those values specified in the validation rule that do not correspond to CakePHP&#8217;s internal values (such as rule or allowEmpty.)</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/webtechnology.wordpress.com/38/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/webtechnology.wordpress.com/38/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webtechnology.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webtechnology.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webtechnology.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webtechnology.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/webtechnology.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/webtechnology.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/webtechnology.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/webtechnology.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webtechnology.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webtechnology.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webtechnology.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webtechnology.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webtechnology.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webtechnology.wordpress.com/38/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&amp;blog=2417486&amp;post=38&amp;subd=webtechnology&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://webtechnology.wordpress.com/2008/04/07/multiple-rules-of-validation-per-field-in-cakephp-12/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4682697a6bd2ad125716a325e07284a2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aseefahmed</media:title>
		</media:content>
	</item>
		<item>
		<title>Browse The File System</title>
		<link>http://webtechnology.wordpress.com/2008/03/28/browse-the-file-system/</link>
		<comments>http://webtechnology.wordpress.com/2008/03/28/browse-the-file-system/#comments</comments>
		<pubDate>Fri, 28 Mar 2008 11:22:47 +0000</pubDate>
		<dc:creator>aseefahmed</dc:creator>
				<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://webtechnology.wordpress.com/?p=36</guid>
		<description><![CDATA[cd changes derectories cd  To your home directory cd .. To your home directory cd - To your previous working directory Listing Directory Contents ls -a  include hidden files ls -l long listing style ls -R Recurse through directories Copying Files &#38; Directories cp -i source destination interactive(ask before overwriting file) cp -R source destination [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&amp;blog=2417486&amp;post=36&amp;subd=webtechnology&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><font color="#993300"><b>cd changes derectories</b></font></p>
<ul>
<li><font color="#339966">cd </font>
<ul>
<li>To your home directory</li>
</ul>
</li>
</ul>
<ul>
<li><font color="#339966">cd ..</font>
<ul>
<li>To your home directory</li>
</ul>
</li>
<li><font color="#339966">cd -</font>
<ul>
<li>To your previous working directory</li>
</ul>
</li>
</ul>
<p><font color="#993300"><b>Listing Directory Contents</b></font></p>
<ul>
<li><font color="#339966">ls -a </font>
<ul>
<li>include hidden files</li>
</ul>
</li>
</ul>
<ul>
<li><font color="#339966">ls -l</font>
<ul>
<li>long listing style</li>
</ul>
</li>
<li><font color="#339966">ls -R</font>
<ul>
<li>Recurse through directories</li>
</ul>
</li>
</ul>
<p><font color="#993300"><b>Copying Files &amp; Directories<br />
</b></font></p>
<ul>
<li><font color="#339966">cp -i source destination</font>
<ul>
<li>interactive(ask before overwriting file)</li>
</ul>
</li>
</ul>
<ul>
<li><font color="#339966">c</font><font color="#339966">p -R source destination</font>
<ul>
<li>recursive (recursively copy an entire directory tree)</li>
</ul>
</li>
</ul>
<p><font color="#993300"><b>Creating &amp; Removing Files<br />
</b></font></p>
<ul>
<li><font color="#339966">touch </font>
<ul>
<li>create empty file or update filetimestamps</li>
</ul>
</li>
</ul>
<ul>
<li><font color="#339966">rm -i </font><font color="#339966"></font>
<ul>
<li>interactive(ask before removing file)</li>
</ul>
</li>
<li><font color="#339966">rm -R</font>
<ul>
<li>recursively (for removing an entire directory)</li>
</ul>
</li>
<li><font color="#339966">rm -f</font>
<ul>
<li>remove by forcing</li>
</ul>
</li>
<li><font color="#339966">mkdir -p</font>
<ul>
<li>make parent directories if needed</li>
</ul>
</li>
</ul>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/webtechnology.wordpress.com/36/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/webtechnology.wordpress.com/36/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/webtechnology.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/webtechnology.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/webtechnology.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/webtechnology.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/webtechnology.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/webtechnology.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/webtechnology.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/webtechnology.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/webtechnology.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/webtechnology.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/webtechnology.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/webtechnology.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/webtechnology.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/webtechnology.wordpress.com/36/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=webtechnology.wordpress.com&amp;blog=2417486&amp;post=36&amp;subd=webtechnology&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://webtechnology.wordpress.com/2008/03/28/browse-the-file-system/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4682697a6bd2ad125716a325e07284a2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">aseefahmed</media:title>
		</media:content>
	</item>
	</channel>
</rss>
