You might want to check out some PHP object-to-relational mapping
libraries. Propel [1] is the first to come to mind, but there are others
just a quick Google search away. If you'd rather write you're own code,
the Data Mapper pattern [2] sounds like a good fit for what you're
trying to do.
The nice thing about the Data Mapper is that your business logic model
classes remain independent of your data gateways. It encapsulates all
the logic required to construct an Article object (for example) from the
array returned by a Zend_Db query or Zend_Db_Table find method. In
SWilk's discussion below, all the load/store methods he describes would
be placed in your data mapper classes rather than your models.
[1] http://propel.phpdb.org/trac/
[2] http://martinfowler.com/eaaCatalog/dataMapper.html
Regards,
Bryce Lohr
SWilk wrote:
iosonogio wrote:
>
>
> Szymon Wilkołazki wrote:
>> Then you have your model doing just Business Logic, and it doesn't
have to care about database logic.
>>
>
>
> Hi,
> this is the way I would like to do.
>
> Here comes the initial problem though.
>
> The dataStore (a Zend_Db_table, an interface, or whatever) used by my
> Business Object now actually uses a Zend_Db to fetch records.
>
> When it fetches a single or a collections of records, ie articles, I
have
> such records in the form of array or stdClass. In the case I use a
Zend_Db_Table dataStore inside my Business Object, I
> would have returned a set of, say, Article_Record (where Article_Record
> extends Zend_Db_Table).
>
> I would need to trasform them (arrays, stdClass or Article_Record),
into
> Article class objects if I want them to have behaviour and actually
be my
> real business object Article.
>
> Hope I explained clearly in the last passage.
>
> How would you manage this ?
>
I am not sure yet, as I said, those was a concept currently developing
in my mind.
Anyway, I would add some methods to my (business model) Article class:
setStore (Article_Record|Other_Source $dataStore); which would set
internal dataStore, and choose proper translation method based on the
$dataStore class.
Two translation methods for each supported dataStore class:
loadFrom{$_storeType} ()
saveTo{$_storeType} ()
two general methods:
load();
save(); - those would check $_storeType and call the above two;
The concrete loadFromArticleRecord would get all the data from record
and set internal properties of Article class. The
saveToArticleRecord() would translate the Article to Article Record.
Anyway, those translation methods would need to know the structure of
ArticleRecord, and possibly should know how to create a Author object
based on the author_id in Article_Record or dependand Author_Record
object. All nuances of translation from records to business objects
should be encapsulated in this two methods.
One could use some Command or Strategy template instead of those
methods, I am just learning templates currently and am not jet sure
how to achieve that with templates.
With this approach, the only place which you need any knowledge of
Article Record are this two methods called when Article is loaded from
data store and when it is saved.
Hope this helps, but keep in mind that this is only my concept which
need some serious thinking before applying to a real project.
Regards,
Szymon Wilkołazki