I’m using Zend Framework and following the design pattern of separating the Data layer from the Domain layer
the problem raises when implementing the methods for the Data mapper
so i implemented the save() which insert & update based on whether domain model contains id property and find() which return the records domain object based on id parameter
but what if i need to
- search all/some rows in a table and return all the columns
- search the same rows and return a mysql COUNT value
should i just directly use the class the inherited the Zend_Db_Table_Abstract for these needs or
should i implement method for every need ?
i’m a little confused on how to divide the functionality of the Data Mapper that will fit my needs and my future needs
You can add individual finder Methods, e.g.
However, that will quickly get out of hand when you need to query multiple columns or want to handle CRUD by arbitrary criteria. You don’t want methods like
The easy solution would be to use
Zend_Db_Table_Selectto create the queries and then pass those to the Data Mapper to execute and map them, e.g. in your DataMapperYou could abstract this further with the Mapper returning and accepting PersonQueryBuilder instead, which hides the SQL Semantics inside and let’s you specify against your Domain Objects instead. It’s more effort though.
Also have a look at the Repository and Specification Pattern.