title: How to execute a manually-written query or stored procedure in ZF2
body: <p>I'm following the tutorial at <a href="http://framework.zend.com/manual/2.0/en/user-guide/database-and-models.html" rel="nofollow">http://framework.zend.com/manual/2.0/en/user-guide/database-and-models.html</a>, and I have set up my <code>Module::getServiceConfig()</code> and my <code>AlbumTable</code> class as they've shown.</p>
  
  <p>Now say I need to execute a stored procedure to get my result set...not a standard <code>SELECT</code>. I still want the result set to be a collection of <code>Album</code> models. How can this be done?</p>
  
  <p>Here's an example of what I've tried:</p>
  
  <p><strong>AlbumTable.php:</strong></p>
  
  <pre><code>public function findByArtist($artist) {
      $adapter = $this-&gt;tableGateway-&gt;getAdapter();
      $result = $adapter-&gt;query(
          "EXEC spGetAlbums @artist = :artist",
          array(':artist' =&gt; $artist)
      );
  
      var_dump($result-&gt;current());
  }
  </code></pre>
  
  <p>The problem with this is that the <code>$result</code> returned is a collection of <code>ArrayObject</code>s...not a collection of <code>Album</code> models. So how can I execute the stored procedure but still get the results according to what I've set up in <code>Module::getServiceConfig()</code>?</p>
  
  <p>Please don't bother asking what my stored procedure is doing...that's irrelevant to this question. Point is, it returns results that are compatible with the <code>Album</code> model.<br /><sub>^ Not meant to be attitude, just trying to save everyone the time of suggesting that I don't need to use a stored procedure.</sub></p>