[Zope] Inserting an object automagically on folder creation

J Cameron Cooper jccooper@jcameroncooper.com
Fri, 28 Feb 2003 14:03:28 -0600


> A) I'd like to create two way to represent data. In the first one the
> data is stored in in ZODB, in the second one the data is stored in a
> (possibly) remote database server.
>
> So I intend to do it this way :
>
> - Zpeople
>   `- ZpeopleItemContainer (inheriting from Folder)
>   `- ZpeopleItem (inheriting from SimpleItem)
>   `- ZpeopleSQL (any pointer on how I can do this)
>
> I'd like ZpeopleSQL and ZpeopleItemContainer to have the same contract
> (ie: a search method with different flavour of search).
>
> Last but not least, 'a' being a ZpeopleSQL instance and 'b' being a
> ZpeopleItemContainer instance. Is it possible through some clever use of
> acquisition to acces the same kind of internal information by the same
> kind of url ?
>
> Example:
> localhost/folder/a/p1/name -> name of people whose id is p1 (DB version)
> localhost/folder/b/p1/name -> name of people whose id is p1 (ZODB 
> version)

You should probably take a look at Pluggable Brains and the Result 
Objects. It allows you to do something quite similar to this. I was able 
to muck up a quick example in a few minutes where I could do:

localhost/folder/b/name/p1/nameview

where there is some table 'data' in my database with a 'name' column 
among some others,
having in its records one where 'p1' is the value of the 'name' field,

where 'b' is an ZSQLMethod in 'folder' that has a 'name' parameter and 
looks like

select * from data
<dtml-sqlgroup where>
 <dtml-sqltest name type="nb" optional>
</dtml-sqlgroup>

where 'nameview' is a DTML Method in 'folder' (or above) looking like

<dtml-var name>

Works fine. I'm sure if you get into it, you can make it look like your 
example above, though I expect it to be easier to change your 'a'-type 
product.

Start in the Zope book chapter on Relational Database Connectivity:
http://www.zope.org/Documentation/Books/ZopeBook/current/RelationalDatabases.stx
pay special attention to the "Traversing to Result Objects" and "Binding 
Classes to Result Objects" sections.

Also take a look at
http://www.zope.org/Members/spinwing/ZSQL_Results
http://www.zope.org/Members/mcdonc/HowTos/direct_traversal

and of course the Zope source, looking for 'Brains' among others.

          --jcc