<?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>A Coding Fool &#187; Zend Framework</title>
	<atom:link href="http://blog.acodingfool.com/tags/zend-framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.acodingfool.com</link>
	<description></description>
	<lastBuildDate>Fri, 22 Jan 2010 00:32:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Zend Framework and Stored Procedures</title>
		<link>http://blog.acodingfool.com/2009/08/21/zend-framework-and-stored-procedures/</link>
		<comments>http://blog.acodingfool.com/2009/08/21/zend-framework-and-stored-procedures/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 15:49:09 +0000</pubDate>
		<dc:creator>acodingfool</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Stored Procedures]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://blog.acodingfool.com/?p=620</guid>
		<description><![CDATA[<p>I'm been playing with the Zend Framework and, whenever I'm faced with a new data abstraction layer [Zend_Db in this case], I cannot help but rethink the <a href="http://www.codinghorror.com/blog/archives/000117.html" target="_blank">great debate over whether to use (or not use) stored procedures</a> [<a href="http://stackoverflow.com/questions/216569/are-the-days-of-the-stored-procedure-numbered" target="_blank">more</a>].</p>]]></description>
			<content:encoded><![CDATA[<a class="download alignright" href="http://blog.acodingfool.com/wp-content/uploads/2009/08/Procedures.txt" target="_blank">Download</a>
<p>I'm been playing with the Zend Framework and, whenever I'm faced with a new data abstraction layer [Zend_Db in this case], I cannot help but rethink the <a href="http://www.codinghorror.com/blog/archives/000117.html" target="_blank">great debate over whether to use (or not use) stored procedures</a> [<a href="http://stackoverflow.com/questions/216569/are-the-days-of-the-stored-procedure-numbered" target="_blank">more</a>].</p>
<p>I must admit that I've been predominantly in the "stored procedure" camp (and I won't even consider embedding a query into my code). But ever since I started to use LINQ to SQL (in C# and .NET), I've begun to rethink this philosophy and have begun to find a place for data abstraction layers (DALs) and object relational mappings (ORMs). And while I find that Zend_Db covers 95% of my database needs, it is still very difficult to perform very complex queries or any sort of complex data manipulation (ones that are best done in the database than in code).</p>
<p>And while it is possible to execute stored procedures using the underlying database adapters within Zend_Db, Zend_Db itself does not contain any classes for use with stored procedures. As a result, I have created one and would like to share it with you.</p>

<div class="code" style="white-space: wrap;"><span style="color: #0000ff;">&lt;?php</span><br />
<br />
abstract <span style="color: #0000ff;">class</span> App_Db_Procedures<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//==============================================================================</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// App_Db_Procedures variables</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//==============================================================================</span><br />
<br />
&nbsp; <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">static</span> <span style="color: #000000;">$_instances</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">static</span> <span style="color: #000000;">$_defaultDb</span>;<br />
&nbsp; <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">static</span> <span style="color: #000000;">$_defaultCache</span> = <span style="color: #0000ff;">null</span>;<br />
<br />
&nbsp; <span style="color: #0000ff;">protected</span> <span style="color: #000000;">$_db</span> = <span style="color: #0000ff;">null</span>;<br />
&nbsp; <span style="color: #0000ff;">protected</span> <span style="color: #000000;">$_cache</span> = <span style="color: #0000ff;">null</span>;<br />
&nbsp; <span style="color: #0000ff;">protected</span> <span style="color: #000000;">$_metadata</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #0000ff;">protected</span> <span style="color: #000000;">$_prefix</span> = <span style="color: #ac1515;">''</span>;<br />
&nbsp; <span style="color: #0000ff;">protected</span> <span style="color: #000000;">$_query</span> = <span style="color: #0000ff;">null</span>;<br />
<br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// App_Db_Procedures :: getDefaultAdapter()</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// Gets the default database adapter</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
<br />
&nbsp; <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> getDefaultAdapter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> self::<span style="color: #000000;">$_defaultDb</span>;<br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// App_Db_Procedures :: setDefaultAdapter()</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// $db: The database adapter to set</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// Sets the default database adapter</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
<br />
&nbsp; <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> setDefaultAdapter<span style="color: #000000;">&#40;</span><span style="color: #000000;">$db</span> = <span style="color: #0000ff;">null</span><span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; self::<span style="color: #000000;">$_defaultDb</span> = self::_setupAdapter<span style="color: #000000;">&#40;</span><span style="color: #000000;">$db</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// App_Db_Procedures -&gt; getAdapter()</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// Gets the database adapter</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; &nbsp; <br />
&nbsp; <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> getAdapter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">$this</span>-&gt;_db === <span style="color: #0000ff;">null</span> &amp;&amp; self::<span style="color: #000000;">$_defaultDb</span> !== <span style="color: #0000ff;">null</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> self::<span style="color: #000000;">$_defaultDb</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #000000;">$this</span>-&gt;_db;<br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// App_Db_Procedures -&gt; setAdapter()</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// $db: The database adapter to set</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// Sets the database adapter</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
<br />
&nbsp; <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setAdapter<span style="color: #000000;">&#40;</span><span style="color: #000000;">$db</span><span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">$this</span>-&gt;_db = self::_setupAdapter<span style="color: #000000;">&#40;</span><span style="color: #000000;">$db</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #000000;">$this</span>;<br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// App_Db_Procedures :: _setupAdapter()</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// $db: The database adapter to be set up</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// Sets up the database adapter</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
<br />
&nbsp; <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> _setupAdapter<span style="color: #000000;">&#40;</span><span style="color: #000000;">$db</span><span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">$db</span> === <span style="color: #0000ff;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">null</span>;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0000ff;">is_string</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">$db</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">$db</span> = Zend_Registry::<span style="color: #000000;">get</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">$db</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>!<span style="color: #000000;">$db</span> instanceof Zend_Db_Adapter_Abstract<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; throw <span style="color: #0000ff;">new</span> Zend_Db_Exception<span style="color: #000000;">&#40;</span><span style="color: #ac1515;">'Argument must be of type Zend_Db_Adapter_Abstract, '</span> . <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ac1515;">'or a Registry key where a Zend_Db_Adapter_Abstract '</span> .<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ac1515;">'object is stored'</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #000000;">$db</span>;<br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// App_Db_Procedures :: getDefaultMetadataCache()</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// Gets the default metadata cache</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; &nbsp; <br />
&nbsp; <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> getDefaultMetadataCache<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> self::<span style="color: #000000;">$_defaultCache</span>;<br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// App_Db_Procedures :: setDefaultMetadataCache()</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// $cache: The cache to set</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// Sets the default metadata cache</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
<br />
&nbsp; <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> setDefaultMetadataCache<span style="color: #000000;">&#40;</span><span style="color: #000000;">$cache</span> = <span style="color: #0000ff;">null</span><span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; self::<span style="color: #000000;">$_defaultCache</span> = self::_setupMetadataCache<span style="color: #000000;">&#40;</span><span style="color: #000000;">$cache</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// App_Db_Procedures -&gt; getMetadataCache()</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// Gets the metadata cache</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
<br />
&nbsp; <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> getMetadataCache<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">$this</span>-&gt;_cache === <span style="color: #0000ff;">null</span> &amp;&amp; self::<span style="color: #000000;">$_defaultCache</span> !== <span style="color: #0000ff;">null</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> self::<span style="color: #000000;">$_defaultCache</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #000000;">$this</span>-&gt;_cache;<br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// App_Db_Procedures -&gt; setMetadataCache()</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// $cache: The cache to set</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// Sets the metadata cache</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; &nbsp; <br />
&nbsp; <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setMetadataCache<span style="color: #000000;">&#40;</span><span style="color: #000000;">$cache</span> = <span style="color: #0000ff;">null</span><span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">$this</span>-&gt;_cache = self::_setupMetadataCache<span style="color: #000000;">&#40;</span><span style="color: #000000;">$cache</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #000000;">$this</span>;<br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// App_Db_Procedures :: _setupMetadataCache()</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// $cache: The cache to be set up</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// Sets up the metadata cache</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
<br />
&nbsp; <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> _setupMetadataCache<span style="color: #000000;">&#40;</span><span style="color: #000000;">$cache</span> = <span style="color: #0000ff;">null</span><span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">$cache</span> === <span style="color: #0000ff;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">null</span>;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0000ff;">is_string</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">$cache</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">$cache</span> = Zend_Registry::<span style="color: #000000;">get</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">$cache</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>!<span style="color: #000000;">$cache</span> instanceof Zend_Cache_Core<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; throw <span style="color: #0000ff;">new</span> Zend_Db_Exception<span style="color: #000000;">&#40;</span><span style="color: #ac1515;">'Argument must be of type Zend_Cache_Core, '</span> .<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ac1515;">'or a Registry key where a Zend_Cache_Core '</span> .<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ac1515;">'object is stored'</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #000000;">$cache</span>;<br />
&nbsp; <span style="color: #000000;">&#125;</span>&nbsp;<br />
<br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// App_Db_Procedures -&gt; _setupMetadata()</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// Discovers metadata (from the cache or retrieved and added to the cache)</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; &nbsp; <br />
&nbsp; <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">function</span> _setupMetadata<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0000ff;">count</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">$this</span>-&gt;_metadata<span style="color: #000000;">&#41;</span> &gt; <span style="color: #ff8000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #0000ff;">return</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #000000;">$cache</span> = <span style="color: #000000;">$this</span>-&gt;<span style="color: #000000;">getMetadataCache</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">$cache</span> !== <span style="color: #0000ff;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">$id</span> = <span style="color: #0000ff;">get_class</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> . <span style="color: #ac1515;">'_'</span> . <span style="color: #0000ff;">md5</span><span style="color: #000000;">&#40;</span><span style="color: #ac1515;">&quot;$this-&gt;_prefix&quot;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">$cache</span> === <span style="color: #0000ff;">null</span> || <span style="color: #000000;">&#40;</span>!<span style="color: #000000;">$meta</span> = <span style="color: #000000;">$cache</span>-&gt;<span style="color: #000000;">load</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">$id</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span>&nbsp; &nbsp;&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">$meta</span> = <span style="color: #0000ff;">array_merge</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">$this</span>-&gt;<span style="color: #000000;">getAdapter</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>-&gt;<span style="color: #000000;">fetchAll</span><span style="color: #000000;">&#40;</span><span style="color: #ac1515;">'SHOW PROCEDURE STATUS'</span>, <span style="color: #0000ff;">null</span>, Zend_Db::<span style="color: #000000;">FETCH_OBJ</span><span style="color: #000000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">$this</span>-&gt;<span style="color: #000000;">getAdapter</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>-&gt;<span style="color: #000000;">fetchAll</span><span style="color: #000000;">&#40;</span><span style="color: #ac1515;">'SHOW FUNCTION STATUS'</span>, <span style="color: #0000ff;">null</span>, Zend_Db::<span style="color: #000000;">FETCH_OBJ</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">$cache</span> !== <span style="color: #0000ff;">null</span> &amp;&amp; !<span style="color: #000000;">$cache</span>-&gt;<span style="color: #000000;">save</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">$meta</span>, <span style="color: #000000;">$id</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; throw <span style="color: #0000ff;">new</span> Zend_Db_Exception<span style="color: #000000;">&#40;</span><span style="color: #ac1515;">'Failed saving metadata to metadataCache'</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000;">$this</span>-&gt;_metadata = <span style="color: #000000;">$meta</span>;<br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// App_Db_Procedures -&gt; _setupMetadataParams()</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// Discovers the number of parameters for the specified procedure and adds it </span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// to the cache</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; &nbsp; <br />
&nbsp; <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">function</span> _setupMetadataParams<span style="color: #000000;">&#40;</span><span style="color: #000000;">$proc</span><span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>!<span style="color: #0000ff;">isset</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">$proc</span>-&gt;<span style="color: #000000;">ParamCount</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">$query</span> = <span style="color: #000000;">$this</span>-&gt;<span style="color: #000000;">getAdapter</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>-&gt;<span style="color: #000000;">prepare</span><span style="color: #000000;">&#40;</span><span style="color: #ac1515;">&quot;SHOW CREATE $proc-&gt;Type $proc-&gt;Name&quot;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">$query</span>-&gt;<span style="color: #000000;">execute</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">$query</span>-&gt;<span style="color: #000000;">bindColumn</span><span style="color: #000000;">&#40;</span><span style="color: #ff8000;">3</span>, <span style="color: #000000;">$procBody</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">$query</span>-&gt;<span style="color: #000000;">fetch</span><span style="color: #000000;">&#40;</span>Zend_Db::<span style="color: #000000;">FETCH_BOUND</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">$query</span>-&gt;<span style="color: #000000;">closeCursor</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">$procBody</span> === <span style="color: #0000ff;">null</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; throw <span style="color: #0000ff;">new</span> Zend_Db_Exception<span style="color: #000000;">&#40;</span><span style="color: #ac1515;">'Unable to cache stored procedure parameters'</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">preg_match</span><span style="color: #000000;">&#40;</span><span style="color: #ac1515;">'/(?:'</span> . <span style="color: #000000;">$proc</span>-&gt;<span style="color: #000000;">Type</span> . <span style="color: #ac1515;">'.+<span style="color: #008080; font-weight: bold;">\(</span>(.*)<span style="color: #008080; font-weight: bold;">\)</span><span style="color: #008080; font-weight: bold;">\s</span>BEGIN)/msU'</span>, <span style="color: #000000;">$procBody</span>, <span style="color: #000000;">$matches</span><span style="color: #000000;">&#41;</span>;&nbsp;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0000ff;">count</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">$matches</span><span style="color: #000000;">&#41;</span> != <span style="color: #ff8000;">2</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; throw <span style="color: #0000ff;">new</span> Zend_Db_Exception<span style="color: #000000;">&#40;</span><span style="color: #ac1515;">'Unable to cache stored procedure parameters'</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">$params</span> = <span style="color: #0000ff;">trim</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #ff8000;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0000ff;">empty</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">$params</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">$proc</span>-&gt;<span style="color: #000000;">ParamCount</span> = <span style="color: #ff8000;">0</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">else</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">$params</span> = <span style="color: #0000ff;">explode</span><span style="color: #000000;">&#40;</span><span style="color: #ac1515;">','</span>, <span style="color: #000000;">$params</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">$proc</span>-&gt;<span style="color: #000000;">ParamCount</span> = <span style="color: #0000ff;">count</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">$params</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">$cache</span> = <span style="color: #000000;">$this</span>-&gt;<span style="color: #000000;">getMetadataCache</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">$cache</span> !== <span style="color: #0000ff;">null</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span>&nbsp;&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">$id</span> = <span style="color: #0000ff;">md5</span><span style="color: #000000;">&#40;</span><span style="color: #ac1515;">&quot;procedures.$this-&gt;_prefix&quot;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>!<span style="color: #000000;">$cache</span>-&gt;<span style="color: #000000;">save</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">$this</span>-&gt;_metadata, <span style="color: #000000;">$id</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw <span style="color: #0000ff;">new</span> Zend_Db_Exception<span style="color: #000000;">&#40;</span><span style="color: #ac1515;">'Failed saving metadata to metadataCache'</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// App_Db_Procedures -&gt; _getProcedure()</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// $name: The name of the procedure to get</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// Search the metadata for a procedure with the specified name</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; &nbsp; <br />
&nbsp; <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">function</span> _getProcedure<span style="color: #000000;">&#40;</span><span style="color: #000000;">$name</span><span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">$this</span>-&gt;_setupMetadata<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">$this</span>-&gt;_metadata <span style="color: #0000ff;">as</span> <span style="color: #000000;">$proc</span><span style="color: #000000;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">$proc</span>-&gt;<span style="color: #000000;">Name</span> == <span style="color: #000000;">$name</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">$this</span>-&gt;_setupMetadataParams<span style="color: #000000;">&#40;</span><span style="color: #000000;">$proc</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #000000;">$proc</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">null</span>;&nbsp; &nbsp; <br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// App_Db_Procedures -&gt; __call()</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// $method: The name of the method to call</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// $params: An array of parameter used to invoke the method</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// Calls the specified method</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; &nbsp; <br />
&nbsp; <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __call<span style="color: #000000;">&#40;</span><span style="color: #000000;">$method</span>, <span style="color: #000000;">$params</span><span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0000ff;">method_exists</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">$this</span>, <span style="color: #000000;">$method</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">call_user_func_array</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">array</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">$this</span>, <span style="color: #000000;">$method</span><span style="color: #000000;">&#41;</span>, <span style="color: #000000;">$params</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000;">$name</span> = <span style="color: #000000;">$this</span>-&gt;_prefix . <span style="color: #0000ff;">strtolower</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">preg_replace</span><span style="color: #000000;">&#40;</span><span style="color: #ac1515;">'/([A-Z][a-z0-9]+)/U'</span>, <span style="color: #ac1515;">'_$1'</span>, <span style="color: #000000;">$method</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">$proc</span> = <span style="color: #000000;">$this</span>-&gt;_getProcedure<span style="color: #000000;">&#40;</span><span style="color: #000000;">$name</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">$proc</span> === <span style="color: #0000ff;">null</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; throw <span style="color: #0000ff;">new</span> Zend_Db_Exception<span style="color: #000000;">&#40;</span><span style="color: #ac1515;">&quot;The requested procedure '$name' is not found&quot;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000;">$count</span> = <span style="color: #0000ff;">count</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">$params</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">$binds</span> = <span style="color: #000000;">&#40;</span><span style="color: #000000;">$proc</span>-&gt;<span style="color: #000000;">ParamCount</span> == <span style="color: #ff8000;">0</span> ? <span style="color: #ac1515;">''</span>: <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">implode</span><span style="color: #000000;">&#40;</span><span style="color: #ac1515;">', '</span>, <span style="color: #0000ff;">array_fill</span><span style="color: #000000;">&#40;</span><span style="color: #ff8000;">0</span>, <span style="color: #000000;">$proc</span>-&gt;<span style="color: #000000;">ParamCount</span>, <span style="color: #ac1515;">'?'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">$count</span> &lt; <span style="color: #000000;">$proc</span>-&gt;<span style="color: #000000;">ParamCount</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">$params</span> = <span style="color: #0000ff;">array_merge</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">$params</span>, <span style="color: #0000ff;">array_fill</span><span style="color: #000000;">&#40;</span><span style="color: #ff8000;">0</span>, <span style="color: #000000;">$proc</span>-&gt;<span style="color: #000000;">ParamCount</span> - <span style="color: #000000;">$count</span>, <span style="color: #0000ff;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">$proc</span>-&gt;<span style="color: #000000;">Type</span> == <span style="color: #ac1515;">'PROCEDURE'</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">$this</span>-&gt;_query = <span style="color: #000000;">$this</span>-&gt;<span style="color: #000000;">getAdapter</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>-&gt;<span style="color: #000000;">prepare</span><span style="color: #000000;">&#40;</span><span style="color: #ac1515;">&quot;call $name($binds)&quot;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">$this</span>-&gt;_query-&gt;<span style="color: #000000;">execute</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">$params</span><span style="color: #000000;">&#41;</span>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #000000;">$this</span>-&gt;_query;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">else</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">$this</span>-&gt;_query = <span style="color: #000000;">$this</span>-&gt;<span style="color: #000000;">getAdapter</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>-&gt;<span style="color: #000000;">prepare</span><span style="color: #000000;">&#40;</span><span style="color: #ac1515;">&quot;SELECT $name($binds)&quot;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">$this</span>-&gt;_query-&gt;<span style="color: #000000;">execute</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">$params</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #000000;">$this</span>-&gt;_query-&gt;<span style="color: #000000;">fetchColumn</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// App_Db_Procedures :: __callStatic()</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// $method: The name of the method to call</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// $params: An array of parameter used to invoke the method</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// Calls the specified method on the current instance of the object</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
<br />
&nbsp; <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> __callStatic<span style="color: #000000;">&#40;</span><span style="color: #000000;">$method</span>, <span style="color: #000000;">$params</span><span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">call_user_func_array</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">array</span><span style="color: #000000;">&#40;</span>self::<span style="color: #000000;">getInstance</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #000000;">$method</span><span style="color: #000000;">&#41;</span>, <span style="color: #000000;">$params</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; <span style="color: #000000;">&#125;</span>&nbsp;<br />
<br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// App_Db_Procedures :: getInstance()</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// Gets the current instance of the object</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; &nbsp; <br />
&nbsp; <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> getInstance<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">$child</span> = get_called_class<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>!<span style="color: #0000ff;">isset</span><span style="color: #000000;">&#40;</span>self::<span style="color: #000000;">$_instances</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">$child</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> self::<span style="color: #000000;">$_instances</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">$child</span><span style="color: #000000;">&#93;</span> = <span style="color: #0000ff;">new</span> <span style="color: #000000;">$child</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> self::<span style="color: #000000;">$_instances</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">$child</span><span style="color: #000000;">&#93;</span>;<br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">?&gt;</span></div>

<p>This class is designed to automatically discover and cache the available stored procedure definitions and allows these procedures to be executed without the need to explicitly define a function for each (but you will still need to set up the default database adapter and cache in your bootstrap). Through the use of PHP's __call magic function, the class will attempt to execute the stored procedure matching the called function (in addition, it ensures that the count of parameters matches that of the stored procedure). All you need to do it create a class that extends this class in order to begin using your stored procedures.</p>

<div class="code" style="white-space: wrap;"><span style="color: #0000ff;">class</span> Public_Model_Procedures_Auth <span style="color: #0000ff;">extends</span> App_Db_Procedures<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//==============================================================================</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// Public_Model_Procedures_Auth variables</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//==============================================================================</span><br />
<br />
&nbsp; <span style="color: #0000ff;">protected</span> <span style="color: #000000;">$_prefix</span> = <span style="color: #ac1515;">'auth_'</span>;<br />
<span style="color: #000000;">&#125;</span></div>

<p>Child classes may define a $_prefix variable to indicate any prefix you may use in your stored procedure names. In addition, the class will convert the called camel-cased method name to a lowercased underscored-delimited stored procedure name (this is my preferred naming convention for database entities). Therefore, functions will be mapped to stored procedure like so:</p>

<div class="code" style="white-space: wrap;">Public_Model_Procedures_Auth::<span style="color: #000000;">getUserCredentials</span><span style="color: #000000;">&#40;</span>...<span style="color: #000000;">&#41;</span> =&gt; auth_get_user_credentials<span style="color: #000000;">&#40;</span>...<span style="color: #000000;">&#41;</span><br />
Public_Model_Procedures_Auth::<span style="color: #000000;">authenticateUser</span><span style="color: #000000;">&#40;</span>...<span style="color: #000000;">&#41;</span> =&gt; auth_authenticate_user<span style="color: #000000;">&#40;</span>...<span style="color: #000000;">&#41;</span></div>

<p>It should be noted, that this file is designed to use PHP 5.3 as it utilizes both the __callStatic() and get_called_class() functions. This allows stored procedures to be called via static methods and allows singleton instances to be created from within the abstract parent class. In older versions of PHP, you will need to make the following changes to your child classes:</p>

<div class="code" style="white-space: wrap;"><span style="color: #0000ff;">class</span> Public_Model_Procedures_Auth <span style="color: #0000ff;">extends</span> App_Db_Procedures<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//==============================================================================</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// Public_Model_Procedures_Auth variables</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//==============================================================================</span><br />
<br />
&nbsp; <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">static</span> <span style="color: #000000;">$_instance</span> = <span style="color: #0000ff;">null</span>;<br />
&nbsp; <span style="color: #0000ff;">protected</span> <span style="color: #000000;">$_prefix</span> = <span style="color: #ac1515;">'auth_'</span>;<br />
<br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// Public_Model_Procedures_Auth :: getInstance()</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//----------------------------------------------------------------------------</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">// Gets the current instance of the object</span><br />
&nbsp; <span style="color: #008000; font-style: italic;">//============================================================================</span><br />
&nbsp; &nbsp; <br />
&nbsp; <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> getInstance<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>self::<span style="color: #000000;">$_instance</span> == <span style="color: #0000ff;">null</span><span style="color: #000000;">&#41;</span> self::<span style="color: #000000;">$_instance</span> = <span style="color: #0000ff;">new</span> self<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> self::<span style="color: #000000;">$_instance</span>;<br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div>

<p>With this modification, stored procedures must be executed like so:</p>

<div class="code" style="white-space: wrap;">Public_Model_Procedures_Auth::<span style="color: #000000;">getInstance</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>-&gt;<span style="color: #000000;">getUserCredentials</span><span style="color: #000000;">&#40;</span>...<span style="color: #000000;">&#41;</span><br />
Public_Model_Procedures_Auth::<span style="color: #000000;">getInstance</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>-&gt;<span style="color: #000000;">authenticateUser</span><span style="color: #000000;">&#40;</span>...<span style="color: #000000;">&#41;</span></div>

<p>While I may do most of my database work with DALs (and the debate over stored procedures still rages on), I still find the need to stored procedures now and then and this class will give you a very simple interface to your procedures.</p>
<p>I hope you find it helpful.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.acodingfool.com/2009/08/21/zend-framework-and-stored-procedures/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
