[Zope] [ADVANCED] How do YOU guys do many-to-many in Zope without relational?

Casey Duncan c.duncan@nlada.org
Mon, 22 Oct 2001 16:08:20 -0400


On Monday 22 October 2001 03:44 pm, Max M allegedly wrote:
> I am in the situation where I have to do what corresponds to a many-to-many
> relation in Zope.
>
> I basically have two zClassses. students and subjects. There can be many
> subjects, and in any subject there can be any number of students.
>
> What I do now is having a studentlist under each subject. But this doesn't
> really work because any single student can belong to any number of
> subjects.
>
> So if this was an ordinary relational db it would be many to many.
>
> Now I considder making a global student list and then do a recursive search
> for subjects in the site, so that each student can be related to a subject
> with a selectbox or somesuch.
>
> But if someone renames, deletes or moves a subject I am f.*?d Or the
> student is. The relation to the subject is invalidated.
>
> I have never seen it used, but would it be possible to store the "global
> object id" in a list in a user object, and then get the path to the subject
> object from that?
>
> Or should I make the subject a global list to? That way I only have to
> check if the object exists or not. Not where it is in the object hierachy.
>
> This sort of defeats the purpose of Zope's tree like structure. Doesn't it?
>
> So, how do YOU guys do many-to-many in Zope ?
>
> regards Max M

They way to do this is to create a lines property in the student and store 
the ids of the related subjects there. You use a ZCatalog and a keyword index 
on this property to quickly find which students belong to which subject.

Now, this won't fix the renaming issue (although it does fix relocating, 
assuming the ids are otherwise unique), but you could get around that two 
ways:

1. Don't rename! Treat the id as the primary key of the subject.
2. Assign another fixed key as a property (or general attribute) of the 
subject for use as the unique identifier and don't ever change it for the 
life of the object. You will need to use a ZCatalog to make lookups on this 
id efficient, but this would allow you to put the object anywhere you like, 
and rename them without screwing up the relationships. Only deletion would 
break it.

hth,
/---------------------------------------------------\
  Casey Duncan, Sr. Web Developer
  National Legal Aid and Defender Association
  c.duncan@nlada.org
\---------------------------------------------------/