RE: [Zope-dev] using persistent storage inside Zope
-----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
Quoting Michel Pelletier <michel@digicool.com>:
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, thanks for the help! i think i am still missing something, though-- if it's a hack, it's because i don't know what i'm doing! :-) :-) i would like to make this a Zope object, but... how do i make a Zope object from inside of Zope...? specifically, what i think i need to do at the time my product is initialized, i need to check for this storage object, and if doesn't exist, create it. (this would be in the __init__.py file for the product, i think.) then, when instances of my product are created, i want them to be able to add data to this storage object. how do they know where the storage object is, and how do they call its methods...?? please excuse me if these are elementary questions, i'm pretty new to working with Zope. cheers adam -- Adam Feuer adamf@pobox.com
At 22:31 20/09/99 , Adam Feuer wrote:
michel, thanks for the help! i think i am still missing something, though-- if it's a hack, it's because i don't know what i'm doing! :-) :-)
i would like to make this a Zope object, but... how do i make a Zope object from inside of Zope...? specifically, what i think i need to do at the time my product is initialized, i need to check for this storage object, and if doesn't exist, create it. (this would be in the __init__.py file for the product, i think.)
then, when instances of my product are created, i want them to be able to add data to this storage object. how do they know where the storage object is, and how do they call its methods...??
please excuse me if these are elementary questions, i'm pretty new to working with Zope.
Just use acquisition. You give your object the id Foo, then you can address it with self.Foo (or just Foo in DTML). -- Martijn Pieters, Web Developer | Antraciet http://www.antraciet.nl | Tel: +31-35-7502100 Fax: +31-35-7502111 | mailto:mj@antraciet.nl http://www.antraciet.nl/~mj | PGP: http://wwwkeys.nl.pgp.net:11371/pks/lookup?op=get&search=0xA8A32149 ------------------------------------------
participants (3)
-
Adam Feuer -
Martijn Pieters -
Michel Pelletier