2009年2月11日星期三

[fw-db] Redundant information updates / Zend_Db_Mapper

Hi there,

I've a question regarding best practices for updating redundant
information without using triggers. I know I could use triggers for
simple tasks, but it's not always an option.

Let's assume I have folders and messages with each folder having a
numberOfMessages property. Thus, everytime I'm adding or deleting a
message I have to adjust this property (also when moving a message from
one folder to another).

Currently I'm working with Zend_Db_Table_Row_Abstract derived "value
objects" (Row_Message and Row_Folder) and Zend_Db_Table_Abstract derived
"gateway objects" (Table_Message and Table_Folder).
I'm also using two models called Model_Message and (surprise!) Model_Folder.

The message model has methods like
- add(array $data) : void
- delete($primaryKey) : void
- get($primaryKey) : Message_Row
- move(Row_Message $message, $folderId) : void
- moveToSpam(Row_Message $message) : void

Now the question is: where should I update the folder's message count?

First I implemented it in Model_Message's add(), delete() and move()
methods, but that seemed to be a bit naive, because one could simple do
something like
$message = $messageModel->get(17);
$message->folder_id = 3;
$message->save();
and nothing got updated in the old and new folder records.

So I moved the count update stuff to Row_Message using _postDelete(),
_postInsert() and _postUpdate() methods and doing some plain vanilla
queries to update the folder record(s). (I could've used Row_Folder, but
that would be a bit like using a sledgehammer to crack a nut, I thought).

What do you think about this approach? Is it "common"?

And what about this issue regarding the proposed Zend_Db_Mapper?
http://framework.zend.com/wiki/display/ZFPROP/Zend_Db_Mapper+-+Benjamin+Eberlei
Will there be a possibility to achieve things like these?

Looking forward to your feedback :-)

Best regards,
Moritz

没有评论: