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

<channel>
	<title>blog.log2e.com &#187; Remoting</title>
	<atom:link href="http://blog.log2e.com/tag/remoting/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.log2e.com</link>
	<description>Tutorials and Code Snippets</description>
	<lastBuildDate>Tue, 25 May 2010 12:47:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Using AMFPHP with ADOdb</title>
		<link>http://blog.log2e.com/2008/05/05/using-amfphp-with-adodb/</link>
		<comments>http://blog.log2e.com/2008/05/05/using-amfphp-with-adodb/#comments</comments>
		<pubDate>Mon, 05 May 2008 11:15:09 +0000</pubDate>
		<dc:creator>Stefan Schmalhaus</dc:creator>
				<category><![CDATA[ADOdb]]></category>
		<category><![CDATA[AMFPHP]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Cairngorm]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Remoting]]></category>

		<guid isPermaLink="false">http://blog.log2e.com/?p=1</guid>
		<description><![CDATA[Very often you want to add functionality from a PHP library to your AMFPHP service classes. This short tutorial shows the use of ADOdb as database abstraction layer and may be especially helpful for beginners with AMFPHP. ADOdb is a widely used database abstraction library that supports a lot of databases. If you want to [...]]]></description>
			<content:encoded><![CDATA[<p>Very often you want to add functionality from a PHP library to your <a title="AMFPHP" href="http://www.amfphp.org" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.amfphp.org?referer=');">AMFPHP</a> service classes. This short tutorial shows the use of <a title="ADOdb" href="http://adodb.sourceforge.net/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/adodb.sourceforge.net/?referer=');">ADOdb</a> as database abstraction layer and may be especially helpful for beginners with AMFPHP.<br />
<span id="more-1"></span><br />
ADOdb is a widely used database abstraction library that supports a lot of databases. If you want to add cross-database support to your Flex application, ADOdb is an excellent choice. But even if you only use MySQL as your bread-and-butter database, ADOdb will greatly help you to simplify your database operations.</p>
<h3>Folder Structure</h3>
<p>First, let&#8217;s take a look at the folder structure on the server. I usually organize my server directories like this:</p>
<p><img style="border: 0pt none; margin: 0px;" src="http://blog.log2e.com/wp-content/uploads/2008/05/amfphp_include_folder.gif" alt="" width="240" height="110" /></p>
<p>There is an <em>includes</em> folder on the same level as the <em>amfphp</em> installation folder. The <em>includes</em> folder contains other folders, one for each PHP library (in this case <a title="ADOdb" href="http://adodb.sourceforge.net/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/adodb.sourceforge.net/?referer=');">ADOdb</a>, <a title="PHPMailer" href="http://phpmailer.codeworxtech.com/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/phpmailer.codeworxtech.com/?referer=');">PHPMailer</a> and <a title="TCPDF" href="http://www.tcpdf.com" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.tcpdf.com?referer=');">TCPDF</a>). Download the ADOdb library <a title="Download ADOdb" href="http://adodb.sourceforge.net/#download" target="_blank" onclick="pageTracker._trackPageview('/outgoing/adodb.sourceforge.net/_download?referer=');">from here</a> and upload the files to the <em>includes/adodb</em> folder.</p>
<h3>globals.php</h3>
<p>It is recommended that you add globally used variables to AMFPHP&#8217;s <em>globals.php</em> file. Since most of your service classes probably need access to the database this file is the best place for configuring your database settings and the path to your external PHP libraries. So let&#8217;s add these lines to the <em>globals.php</em> file:</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">define</span><span class="br0">&#40;</span><span class="st0">&#39;DB_HOST&#39;</span><span class="sy0">,</span> <span class="st0">&#39;localhost&#39;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">define</span><span class="br0">&#40;</span><span class="st0">&#39;DB_USER&#39;</span><span class="sy0">,</span> <span class="st0">&#39;dbuser&#39;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">define</span><span class="br0">&#40;</span><span class="st0">&#39;DB_PASSWORD&#39;</span><span class="sy0">,</span> <span class="st0">&#39;dbpassword&#39;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">define</span><span class="br0">&#40;</span><span class="st0">&#39;DB_NAME&#39;</span><span class="sy0">,</span> <span class="st0">&#39;dbname&#39;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">define</span><span class="br0">&#40;</span><span class="st0">&#39;INCLUDES_PATH&#39;</span><span class="sy0">,</span> <span class="st0">&#39;/base/server/path/includes/&#39;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>Of course, you have to adjust the database and path settings to your needs. The <code>INCLUDES_PATH</code> constant should hold the absolute path to your <em>includes</em> directory on the server.</p>
<h3>Sample Classes</h3>
<p>Let&#8217;s say you have a MySQL database that holds a table with product data. For the sake of simplicity, we assume that there are only two fields in this table, <em>product_id</em> and <em>product_name</em>. </p>
<p>If you use the <a title="Cairngorm" href="http://labs.adobe.com/wiki/index.php/Cairngorm" target="_blank" onclick="pageTracker._trackPageview('/outgoing/labs.adobe.com/wiki/index.php/Cairngorm?referer=');">Cairngorm</a> framework for your Flex application you are familiar with the concept of Value Objects (VOs). Instead of using XML or simple arrays, VOs allow for sending back and forth typed objects between Flex and AMFPHP. We define a simple ProductVO class and place the <em>ProductVO.php</em> file in AMFPHP&#8217;s <em>services</em> folder.</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">&lt;?php</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> ProductVO</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw2">var</span> <span class="re1">$_explicitType</span> <span class="sy0">=</span> <span class="st0">&#39;ProductVO&#39;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw2">var</span> <span class="re1">$productId</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw2">var</span> <span class="re1">$productName</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">?&gt;</span></div>
</li>
</ol>
</div>
<p>Now we add a service class (<em>ProductsService.php</em>) to the <em>services</em> folder. The <em>ProductsService.php</em> file includes the ProductVO class and the ADOdb library. The constructor establishes the database connection (for the syntax take a look at the ADOdb documentation), and the <code>getProducts()</code> mehod retrieves all products from the database, wraps them into ProductVO objects and sends them back to your Flex application.</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">&lt;?php</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">require_once</span><span class="br0">&#40;</span>INCLUDES_PATH<span class="sy0">.</span><span class="st0">&#39;adodb/adodb.inc.php&#39;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">require_once</span><span class="br0">&#40;</span><span class="st0">&#39;ProductVO.php&#39;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> ProductsService</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw2">var</span> <span class="re1">$db</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; * Constructor</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; */</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw2">function</span> ProductsService<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">db</span> <span class="sy0">=</span> NewADOConnection<span class="br0">&#40;</span><span class="st0">&#39;mysql&#39;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">db</span><span class="sy0">-&gt;</span><span class="me1">Connect</span><span class="br0">&#40;</span>DB_HOST<span class="sy0">,</span> DB_USER<span class="sy0">,</span> DB_PASSWORD<span class="sy0">,</span> DB_NAME<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span><span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">db</span><span class="br0">&#41;</span> <span class="kw3">die</span><span class="br0">&#40;</span><span class="st0">&#39;Connection failed&#39;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">db</span><span class="sy0">-&gt;</span><span class="me1">SetFetchMode</span><span class="br0">&#40;</span>ADODB_FETCH_ASSOC<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; * This method retrieves all products from the database</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; */</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw2">function</span> getProducts<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="re1">$products</span> <span class="sy0">=</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="re1">$sql</span> <span class="sy0">=</span> <span class="st0">&#39;SELECT product_id, product_name FROM products&#39;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="re1">$result</span> <span class="sy0">=</span> <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">db</span><span class="sy0">-&gt;</span><span class="me1">Execute</span><span class="br0">&#40;</span><span class="re1">$sql</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re1">$result</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">while</span> <span class="br0">&#40;</span><span class="sy0">!</span><span class="re1">$result</span><span class="sy0">-&gt;</span><span class="me1">EOF</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$productVO</span> <span class="sy0">=</span> <span class="kw2">new</span> ProductVO<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$productVO</span><span class="sy0">-&gt;</span><span class="me1">productId</span> <span class="sy0">=</span> <span class="re1">$result</span><span class="sy0">-&gt;</span><span class="me1">fields</span><span class="br0">&#91;</span><span class="st0">&#39;product_id&#39;</span><span class="br0">&#93;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$productVO</span><span class="sy0">-&gt;</span><span class="me1">productName</span> <span class="sy0">=</span> <span class="re1">$result</span><span class="sy0">-&gt;</span><span class="me1">fields</span><span class="br0">&#91;</span><span class="st0">&#39;product_name&#39;</span><span class="br0">&#93;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">array_push</span><span class="br0">&#40;</span><span class="re1">$products</span><span class="sy0">,</span> <span class="re1">$productVO</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$result</span><span class="sy0">-&gt;</span><span class="me1">MoveNext</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re1">$products</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">?&gt;</span></div>
</li>
</ol>
</div>
<p>In a similar way you can add other PHP libraries to your AMFPHP classes (like the above mentioned PHPMailer and TCPDF libraries). I hope this example helps you to utilize the power of some of the most popular PHP libraries for your Flex applications. If you have suggestions or requests please comment or get in touch with me.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.log2e.com/2008/05/05/using-amfphp-with-adodb/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

