I prefer to move all queries to the model. The fact that the model is sqlalchemy-based is apparent in my controllers only when I turn NoResultFound into 404s, logic that I try not to have in more than a few places anyway.
Controller methods get much shorter (typically no more than a small handful of lines) and the chance of reusing queries is greatly increased. It makes the insertion of additional layers between controller and model much easier too (if you need fancy caching or suchlike, or perhaps for tests). If that's too extreme, maybe there's a half-way house that works for other people; I'd be interested too. Anyway, I hope that helps. Mike [email protected] http://twitter.com/asplake http://positiveincline.com On Jan 20, 11:45 pm, Juliusz Gonera <[email protected]> wrote: > Mariano Mara wrote: > > Looks to me you should be building all those relations at mapper level > > [1][2]. You can map with other classes and even arbitrary sql statements, > > plus you can make some of them lazy loaded so that you won't have to > > retrieve them until the very last minute. > > Of course all of this will be defined in your model, making your > > controller lighter. > > I know all of that (you can see a relationship and append() in my simple > example). That's not what I'm asking. I'm making an app for a database > that I didn't design myself. It has some weird assumptions and I use > reflection (with declarative base). > > The database is quite complex and simply defining relations is not > enough. I have to create a lot of objects which all are related to one, > let's call it, master object in a single controller action, on single > request (after parsing a very long form). Additionaly, I have to make > some checks in the database (query it to e.g. see if something already > exists) during the whole action. That's why I asked if I it's OK (good > practice) to make DB queries inside model's method. > > The action is 100 lines, I think it's too much for a single function. > Question: What are the recommended ways of decomposing such actions in > Pylons (or MVC frameworks in general). > > -- > Juliusz Gonerahttp://juliuszgonera.com/ -- You received this message because you are subscribed to the Google Groups "pylons-discuss" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
