[Zope] Adding ZClass from inside of a ZClass

Jim Washington jwashin@vt.edu
Sat, 22 Jan 2000 15:07:03 +0000


Hi, James

I'll give this a try.

"James W. Howe" wrote:
> 
> I have a ZClass which inherits from ObjectManager.  When I create an
> instance of this ZClass, I also want to automatically create an instance of
> another ZClass that I have created.  I've looked at the How To on doing
> this but things aren't working quite right.  When I add my new instance of
> my ZClass ObjectManager, my second ZClass gets added not to my new ZClass
> instance, but the folder which contains the new ZClass instance.  In other
> words, what I wanted to get was:
> 
> FolderA
>       ZClass1
>            ZClass2
> 
> but I'm getting:
> 
> FolderA
>      ZClass1
>      ZClass2
> 

Not an unusual situation.

> the Add method for ZClass1 looks like this:
> 
> <dtml-call "REQUEST.set('id', 'some id for ZClass1')">
> 
> <dtml-with "ZClass1.createInObjectManager(REQUEST['id'], REQUEST)">
>      <dtml-call
> "propertysheets.instanceProperties.manage_editProperties(REQUEST)">
> 

Ahh, here's where things go a bit awry.  Ask yourself 'Where am I?' and
you will see that the above 'with' hasn't really moved you anywhere.  It
just lets you use the createInObjectManager function.

So here you need to go 'with' the id of the first object.  This is
untested, but you want something like 

<dtml-with some_id_for_ZClass1>

or

<dtml-with "_['some id for ZClass1']">

or maybe a "getItem" ... whichever works.  'Trial and success' reigns
supreme; I never seem to get it right the first time.

>      <dtml-call "REQUEST.set('id', 'some id for ZClass2')">
>      [... other stuff ...]
>      <dtml-call "ZClass2_add(_.None, _, NoRedir=1)">

Recently, I have been pulling out the actual adding code from the _add
methods and using them directly, rather than passing all the parameters.
So for me the above couple of statements would be:

<dtml-with "ZClass2Class.createInObjectManager(id='id_for_ZClass2',
REQUEST=REQUEST)">
</dtml-with>

I have found generally that "REQUEST.set (id="  is a bad thing to do,
since id is so acquireable and difficult to debug if something goes
wrong.  I guess whether to call the _add function or to do it directly
depends on whether you are doing a simple add, or whether you have a
bunch of properties to initialize at the same time.

something like:
<dtml-call "ZClass2_add(_.None, _, NoRedir=1, id='id_for_ZClass2')">

might get you around this problem, too. YMMV.

> </dtml-with>
> <dtml-if NoRedir>
> <dtml-else>
> <dtml-if DestinationURL>
>     [... standard stuff ...]
> </dtml-if>
> </dtml-if>
> 
> What trick am I missing?

Hope this helps.  I assume if I am steering you wrong, someone will
correct any misinformation.  I am still learning some of this after 14
months.

Regards,

-- Jim Washington