[Zope] Re: [Zope-dev] creating one ZClass instance in the constructor of another

Brian Hooper brian@garage.co.jp
Fri, 23 Jul 1999 10:15:55 +0900


Hi, thanks for the response!

Brian Lloyd <Brian@digicool.com> wrote:

> > Hi Zopistas -
> > 
> > This is probably a really easy one, but I was wondering if 
> > anyone knows
> > how to create an instance of one ZClass inside the constructor of a
> > different ZClass.  To illustrate with an example:
> > 
> > I have defined two ZClasses, a ProjectManager and a GuestBook (I
> > reimplemented the old Guestbook example product using ZClasses) as
> > separate Products (since they are not really intrinsically related).
> > I want to use the GuestBook in the ProjectManager, but I can't figure
> > out how to create the GuestBook in the ProjectManager's constructor.
> > 
> > I want to do something like
> > 
> > <!--#call "GuestBook_add(id)"-->
> > 
> > in the ProjectManager_add method; however, since the other Product's
> > names don't seem to be defined in the namespace of ProjectManager_add,
> > this doesn't work.
> > 
> > Can anyone please tell me the magical solution to this problem?
> > 
> > 
> > --Brian Hooper
> 
> This is a little ugly, but it should work (untested). Let me know!
> 
> <!--#call "Control_Panel.Products.Guestbook.Guestbook_add(id)"-->

I tried this (or rather, something like it - sharp observers may have
noticed that I screwed up the question a little; since GuestBook_add is
a DTML Method, it should probably be called <!--#call
"GuestBook_add(REQUEST)"--> after resetting the id in REQUEST to
whatever I want the new object id to be):

       <!--#call "REQUEST.set(id, 'messages')"-->
       <!--#call "Control_Panel.Products.GuestBook.GuestBook_add(REQUEST)"-->

Puzzlingly (at first), there was no error, but also the 'messages'
GuestBook did not appear in my newly created ProjectManager instance. 
Apparently, the GuestBook instance is being created but instead of being
contained in the ProjectManager instance, it is simply returned inside
the ProjectManager_add constructor page.  When I remove the redirect at
the end of the ProjectManager constructor and <!--#var--> instead of
<!--#call--> the above DTML statements, I get a

<GuestBook instance at 80f12e0>

in the ProjectManager_add page.

I guess this has something to do with how createInObjectManager works;
however, I'm still not able to get my noodle around what's going on
there (turns my brain to spaghetti).  Is there any way of skirting around
this problem?  (not the brain spaghetti problem, the instance in an
instance problem)

--Brian