[Zope-dev] using persistent storage inside Zope

Michel Pelletier michel@digicool.com
Mon, 20 Sep 1999 15:53:11 -0400


> -----Original Message-----
> From: Adam Feuer [mailto:adamf@pobox.com]
> Sent: Monday, September 20, 1999 12:54 PM
> To: zope-dev@zope.org
> Subject: [Zope-dev] using persistent storage inside Zope
> 
> 
> zope folks,
>   if this has already been covered somewhere, let me know-- i just
> couldn't find information about it on the Zope site!
> 
>   i'm building a Zope product that needs to store some persistent
> information in the Zope object database. the information needs to be
> accessible to all instances of that product.

I would suggest not making this data a class attribute, and instead make
it an instance of an object that provides a service to your instances
via a protocol you define yourself.  For example, a user folder provides
an authentication server to other zope objects.

>   my approach was: write a class that derives from
> Persistence.Persistent that will store the data and mediate access to
> it. then, in the product's __init__.py file, instantiate this class
> and put the instance into the Zope object database, then call
> get_transaction().commit() to commit it.
>   theoretically, once my instance is in the Zope object database,
> other objects can access it-- adding or subtracting data. and the
> instance will handle committing its own changes, so it won't forget
> anything between restarts of Zope.

Your sorta halfway there, but I would make your 'storage' a full fledged
Zope object, not some sort of product class attribute, if this is what
you mean.  Maybe I'm the one confused.
 
>   my question: how do actually do this?? :-) i found a kludgy way to
> store a class instance in the Zope object database from the product's
> __init__.py file, but i can't figure out how to connect to this object
> from a random instance of my product.
> 
>   amy i going about this all wrong? is there a better way of storing
> and accessing persistent objects from inside of Zope? or if my
> approach is correct, how do actually implement it?
> 
>   here's what i am doing in the product's __init__.py file:
> 
>     # get the ZODB root-- kludge!
>     root=context._ProductContext__app._p_jar.root()
>     # now make the object instance if we need to
>     if root.has_key('FooObject'):
>         print "FooObject already in DB"
>     else:
>         print "made new FooObject."
>         root['FooObject'] = Foo.Foo()
>     get_transaction().commit()
> 
> 
>   the problem seems to be, i don't know how to get access to the Zope
> object database's root object from inside an instance of my product.
> what i want to do inside the product base class, during an add or
> delete of an instance of the product, is something like:
> 
>         # somehow get the root of the Zope ZODB :-)
>         root=Somehow.get.ZODB.root()

	    self._p_jar.root()

>         root['FooObject'].remove_item(someitem)
>         get_transaction().commit()

All persistence objects have an _p_jar attribute which represents the
current database 'connection'.  _p_jar.root() will give you the root
object. 

But what it looks like your doing is stuffing something in the object
database that Zope is not aware about?  Why not just make it a Zope
object?  This smells like a hack.

-Michel