Unfor. due to the nature of the project I cannot simply upgrade SA and
Pylons (as much as I'd like to).

I see the reason for having some context outside the model for
transactional based approaches (like a banking system), but even then
I would move stuff out of a controller and into a compact library or
higher class model.  This is one aspect that other frameworks get
right imho.

For example, I had to modify the "Book" model after 8 months after it
was originally written to allow soft purging (that is when a user
deletes a book, it actually just marks it instead of deleting it..
transparent to the user though.)  This required a SIG amount of time
because I had to add ...filter_by(purge_fl==True)... to every location
where I'm doing a meta.Session.query(Book).filter...  let's just say
it was pretty painful!!  That was over a year ago now and the Pylons
doc has improved a lot (was nearly non existent).

@lasizoillo: As for the link.. I read it.. already doing that stuff..
but thx :)

@Pawel: thanks for the code on paste bin.  unforunate that seems like
i'm really bending the framework more than it wants.. basically id
have to create a model "wrapper" for every model.  I have about 15
models, so thats another 15 wrappers.  Best solution thus far.. gotta
think about it though.

I've also thought about moving all the save logic into the model and
just leaving the actual meta.Session.save() line in the controller.
Of course I have other operations that should be fired before and
after the save (storing meta info in a triple store), so I would need
to call before_save() and after_save() or something (kinda like the
cake framework has it.. but this would have to be explicit).  I def
don't like requiring the developer to HAVE to add these lines and its
dependent on their knowledge.

Anyone else have ideas?

Def appreciate all the QUICK responses!!  wow!  thanks!

j


On Jan 28, 10:40 am, lasizoillo <[email protected]> wrote:
> 2009/1/28 John Brennan <[email protected]>:
>
>
>
> > All the documentation I've found (pylons web site, examples, etc) tell
> > the developer to put all this business logic in the controller and
> > just setup a flimsy old model with the "pass" keyword.  I myself did
> > the same when first learning pylons.  As my app grew, so did my needs.
>
> http://pylonshq.com/docs/en/0.9.7/models/#fancy-classes
>
> Read more ;-)
>
> > I'm trying encapulate all data stuff in the model.  For example, I
> > have a Book object so it would have methods like get, get_all, recent,
> > etc.  Those all work fine, but the problem occurs when trying to
> > create a new Book object.
>
> I like the logic in the model too. Magick properties, search engine
> updates on table updates, ... are better in model.
>
>
>
> > /models/book.py:
> > class Book(object):
>
>     def __init__(self, some_required_field, another_required_field):
>           # I never create the class without required fields. I don't
> need look at book_table definition
>           self.some_required_field = some_required_field
>           self.another_required_field = another_required_field
>
> >    def get_all(self):
> >        query = meta.Session.query(Book).filter_by(user_id=1)
> >        return query.all()
>
> Make this a classmethos is useful.
>
> >    def save(self):
> >        meta.Session.save_or_update(self)
> >        meta.Session.commit()
>
> With some changes (save_or_update now is called add) works fine in my
> SQLAlchemy 5.2 with pylons 0.7. You can make an auto_commit flag for
> execute save in an transaction for a better implementation.
>
> I don't understand the failure. Can you test with another sqlalchemy version?
>
> But, is important this issue? You are not decoupling exception
> handling in your controller and you are interfering with transaction
> issues. This code isn't bad in controller.
>
>
>
> > /models/__init__.py:
> > def init_model(engine):
> >    """
> >    Setup the model.
> >    This gets called by lib.app_globals
> >    """
> >    print "Initializing model..."
> >    sm = orm.sessionmaker(autoflush=True, transactional=True,
> > bind=engine)
> >    meta.engine = engine
> >    meta.Session = orm.scoped_session(sm)
>
> > Here, my get_all() method works like a charm.  BUT the save() method
> > gives me this error:
> > sqlalchemy.exceptions.InvalidRequestError: Class 'type' entity name
> > 'None' has no mapper associated with it
>
> > I can see that maybe the model can't save itself and instead the
> > session should be saving it.  But how do I go about doing that using
> > this modular structure?
>
> > (the suggestion here didn't work for me:
> >http://groups.google.com/group/pylons-discuss/browse_thread/thread/6f...)
> > [I am running Pylons 0.9.7 with SQLAlchemy 0.4.4]
>
> > Thanks!
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to