Combining Zend_Db_Select and Propel 1.3

Propel 1.3 has served me very well over the past few years and I’m absolutely confident that recent projects would not have been delivered so quickly without it. However, when stuff gets a little crazy, you need to hydrate your objects using hand-crafted SQL. Building up a SQL in a variable and passing it between functions can be a nightmare to get right. Since I was already using Zend Framework, I decided to try and get Propel objects back from a Zend_Db_Select generated query. The Relational API in the upcoming 1.5 release of Propel would make this possible without the need for hacks and workarounds…

The quick fix

The quickest way to combine the two is to use something like:

The magic part is the call to “getDriverStatement” which returns the PDOStatement composed by Zend_Db_Statement_Pdo. The only downside to this is that the query will be executed by Zend_Db’s connection to the database, rather than Propel’s. If you can live with that then you may stop reading. If, like me, this sort of negligent waste of resources keeps you awake at night, then you need a rather unsightly hack.

Sleepless nights

Zend_Db doesn’t support any kind of “setConnection” method, where you pass in a PDO object. While Propel does support this, it only accepts an instance of PropelPDO. Rather than extending Propel, I thought the easiest way around this would be to create a Zend_Db adapter which, upon calling getConnection(), simply delegates back to Propel::getConnection().

The new adapter required a little hacking to get it right. There are a few methods to castrate because, understandably, the abstract adapter does a lot of checking to make sure it’s configured correctly. Instead, we’re using a PDO connection, configured elsewhere so any configuration parameters would simply get in the way.

Now we can use the Zend_Db factory (or, because the new adapter does not require any configuration, you could technically just instantiate it directly) to get our new adapter. The select object will use that adapter and we’ve avoided duplicate db connection attempts.

The finished article

Tagged with: , , , ,
Posted in Development, web
0 comments on “Combining Zend_Db_Select and Propel 1.3
2 Pings/Trackbacks for "Combining Zend_Db_Select and Propel 1.3"
  1. […] This post was mentioned on Twitter by Adrian Hardy. Adrian Hardy said: Gasp! my first technical blog post in over 6 months. Mixing Zend_Db_Select with Propel 1.3 : […]

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">