Login | Register
My pages Projects Community openCollabNet

Discussions > dev > [propel-dev] DAO for Propel with Named Query support.

propel
Discussion topic

Back to topic list

[propel-dev] DAO for Propel with Named Query support.

Reply

Author tony_bibbs
Full name Tony Bibbs
Date 2009-04-15 13:24:16 PDT
Message Ok, I've managed to get around to polishing off a DAO implementation
for Propel. What makes this implementation unique is it adds a
powerful alternative to Criteria in what is dubbed Named Queries.
Named Queries are nothing more than XML files containing SQL queries
that are given names. Those names can then be used to execute a
specific query. Why, would anybody want this?

- It's a good middle ground between portability and the desire to
prevent having to learn a new way to write queries (e.g. Criteria or
something like HQL for you Hibernate fans).
- Allows a single SQL query to be reused in multiple parts of your application.
- It keeps your application clean by taking explicit SQL calls out of your code
- Allows for quick regression testing. Because SQL is centralized
into one (or more) files you can easily write a unit test to
regression test all your queries after a database upgrade.
- Just like Criteria it returns Propel objects OR raw data.

How does it work?

- First, all Named Query files are validated against the included XSD
- XML is compiled into a PHP array for faster, subsequent access.
- Calls to a specific query cause the query to be pulled from the
array and then ran through a PDO prepared statement.

There are two parts to this implementation:

1) Generator (optional): we add a new PEER builder class that is DAO
aware. The only thing this builder does special is it adds a
__callStatic() method (new to PHP 5.3) that knows how to call named
queries by name (example later). To use it simply change set the
following in your build.properties:

    propel.builder.peer.class =
propel.engine.builde​r.om.php5.PHP5Apteno​PeerBuilder

2) DAO Implementation. This is the guts of the implementation. The
highlights of how to use are:

- Given this Named Query file: http://tinyurl.com/cnd53t

You'd run a query with the following:

// Do this only once as appropriate for your application.
\Apteno\DAO\Facto​ry::initializeDAO($​this->options);

// Option #1 (preferred)
$matches = AuthorPeer::getAutho​rByFirstName(array('​Foo'));

// Option #2 call on DAO directly
$dao = DAOFactory::getDAO();
$matches = $dao->find('getA​uthorByFirstName',ar​ray('Foo'));

I've published this work at http://pear.apteno.net and you can install
with the following (warning requires PHP 5.3 beta):

#>pear channel-discover pear.apteno.net
#>pear install pear.apteno.net/DAO

Sample code from unit tests:

http://tinyurl.com/ddot7v

I'd like to see some discussion on whether this could be added to
Propel proper or if it is better maintained separately as it is.

--
Tony Bibbs
Phone: 515.554.8046
Twitter, Skype, Facebook: tonybibbs
Web: http://www.tonybibbs.com
         http://www.apteno.net

« Previous message in topic | 1 of 5 | Next message in topic »

Messages

Show all messages in topic

[propel-dev] DAO for Propel with Named Query support. tony_bibbs Tony Bibbs 2009-04-15 13:24:16 PDT
     Re: [propel-dev] DAO for Propel with Named Query support. Cameron Brunner <cameron dot brunner at gmail dot com> Cameron Brunner <cameron dot brunner at gmail dot com> 2009-04-15 13:49:42 PDT
         Re: [propel-dev] DAO for Propel with Named Query support. tony_bibbs Tony Bibbs 2009-04-15 14:51:13 PDT
             Re: [propel-dev] DAO for Propel with Named Query support. Cameron Brunner <cameron dot brunner at gmail dot com> Cameron Brunner <cameron dot brunner at gmail dot com> 2009-04-15 15:20:51 PDT
                 Re: [propel-dev] DAO for Propel with Named Query support. tony_bibbs Tony Bibbs 2009-04-15 19:03:41 PDT
Messages per page: