2009年1月30日星期五

Re: [fw-mvc] Separating Models from DB Access

You're running into the classic object-relational mapping problem. It's
a terribly difficult problem in general, which is why you don't commonly
see solutions in those articles. They're primarily selling the ideals,
and leaving the implementation as an exercise for the reader... :)

Check out Benjamin Eberlei's proposal [1] for a Data Mapper for ZF, and
Martin Fowler's PoEAA for background info.

[1] http://framework.zend.com/wiki/x/OwCQ

Regards,
Bryce Lohr


Jack Sleight wrote:
> Hi,
> I'm currently building a new ZF based app, and on the advice of many
> articles I've read, and my own opinion, want to separate the "model"
> classes from the DB access classes. My problem is, for all the
> articles advising this is the way to do it, I've yet to find an
> example implementation or any advice on how to implement it in the
> real world with ZF.
>
> As far as I understand it, the DB layer should be hidden under the
> model layer, so at no point should the controller be accessing the
> Table or Row classes.
> For example, say I have these classes:
>
> User - This is the user object
>
> User_Table extends Zend_Db_Table
> User_Table_Row extends Zend_Db_Table_Row
>
> So, say we're in a controller and have some data for a new user,
> creating that user would be a case of:
>
> $user = new User($properties);
>
> But what about looking up an existing user? Which of these is right?:
>
> $user = new User($id);
>
> $user = User::find($id);
>
> $users = new Users();
> $user = $users->find($id);
>
> And getting a collection of multiple users? I want an array of User
> objects returned, not User_Table_Row objects, where should these come
> from and how should they be created internally by the model.
>
> Sorry for all the questions, I just really want to do this the best way,
> Thanks,

没有评论: