I was going through the Zend Framework quickstart tutorial, and in their demo code they use a data mapper which uses Zend_DB_Table to access the database (see: http://framework.zend.com/manual/en/learning.quickstart.create-model.html). It seems that Zend_DB_Table doesn't use prepared statements or provide a way to use transactions though. Therefore, I'm leaning towards just using a datamapper directly with a zend_db_adapter. With that in mind, can someone advise as to the situations where Zend_DB_Table would be advantageous? Appreciate your input! Cheers.
3 Answers
In general, I see Zend_Db_Table
as a quick and easy way to do single-table queries.
You can surely do joins with Zend_Db_Table
, but since the class is targeted towards a specific table (after all, it is intended as a Table Data Gateway implementation), all such joins always feel - to me, at least - like an unnatural graft. In that case, I always tend towards straight adapter-based queries with joins, that could include your transaction handling.
Of course, in either case, it is certainly desirable to put all of this behind a model or mapper or service layer. Then, as you change these implementation details- say, start out with a Zend_Db_Table
implementation, but later realize you need to join data from a another table, so you move to adapter-based queries - none of this will affect consumers of the model/mapper/service.
Hope this helps.

- 14,144
- 4
- 42
- 64
-
Thx David, that helps a lot. Also, if i'm using a datamapper, which maps the zend_db_row objects to the domain model, and thus, i'll need to invoke a datamapper::save($domainModel) rather than $zend_db_table_row_object->save(), does that mean all the overhead for zend_db_table_row is being wasted? And therefore, I should just perform the queries directly via database adapters? Additionally, when would anyone use a zend_db_table_row insert method over a zend_db_table insert? Thx so much for your time David, mucho appreciated! – blacktie24 Feb 25 '11 at 23:46
-
I agree that if you are using a $dataMapper->save($domainModel), then the overhead for `Zend_Db_Table_Row` is being wasted. Your second question - calling `insert()` on Zend_Db_Table vs calling it on `Zend_db_Table_Row` is really interesting. I'll have to poke around a bit. Anyone else with insight on this particular point, I'd also be interested to hear. – David Weinraub Feb 26 '11 at 07:00
The way I see it, you want your code to be as Object Orientated as possible for maintainability and testability.
I guess it depends what other solution you would implement instead, but if the alternative is raw, bespoke SQL queries scattered arbitrarily around your code base and being passed around as arrays with whatever key values seemed appropriate in a particular instance, mappers and table gateways are much more structured. Your code will be more reusable.
Also, If you have an object representing a table row, you can dummy that object for unit testing.
It depends what your asking I guess, I'm being pretty high level and listing the advantages (as I understand them) against not using a model pattern at all...
Anyway, hope it helps :)

- 175
- 5
- 15
-
Hey Peter, really appreciate the input! I think I was a bit unclear though, and have since edited my post title and description. I am definitely planning on using a datamapper, but i'm wondering if I should use the Zend_Db_Table with it or just use the Zend_Db_Adapter directly? – blacktie24 Feb 25 '11 at 09:03
-
Just using the adapter can get messy. Its better to model your tables and deal with those in your mappers. Then you get to use table find, insert etc. to interact with your data :) I feel like everything's simpler if your database structure is modelled in your application. – PeterL Feb 25 '11 at 09:47
-
Hey Peter, that makes a lot of senses actually. Posted this followup above, but will repost again. If i'm using a datamapper, which maps the zend_db_row objects to the domain model, and thus, i'll need to invoke a datamapper::save($domainModel) rather than $zend_db_table_row_object->save(), does that mean all the overhead for zend_db_table_row is being wasted? And therefore, I should just perform the queries directly via database adapters? Additionally, when would anyone use a zend_db_table_row insert method over a zend_db_table insert? Thx again for your help Peter! – blacktie24 Feb 25 '11 at 23:50
-
(sorry for the belated response I've been on holiday) Fair warning: I'm not convinced this is right but this is how it seems to me :p OK datamapper save would use zend_db_table save, as the datamapper would itself have a zend_db_table. You're also unlikely to call it with a model argument, but i guess that depends how you're app is structured. PTO... – PeterL Mar 07 '11 at 08:55
-
When you use zend db table find you get a table row back (or a rowset), thats what is saved. You don't need to talk about / explicitly model rows unless you want to override the default behaviour some way. i.e. don't model a row unless you need to and don't explicitly work with rows unless you arrive at a row through a table. But effectively you're always working with rows. Does that make sense?? :) – PeterL Mar 07 '11 at 08:59
- For example if you want to modify data from db
- Or if you need to send mails to peoples on insert records and so on..

- 3,545
- 1
- 28
- 30