[Modeling-cvs] ProjectModeling/Modeling/doc/UserGuide DefiningaModel.tex,1.36,1.37
Status: Abandoned
Brought to you by:
sbigaret
From: <sbi...@us...> - 2003-08-31 17:32:29
|
Update of /cvsroot/modeling/ProjectModeling/Modeling/doc/UserGuide In directory sc8-pr-cvs1:/tmp/cvs-serv18199/Modeling/doc/UserGuide Modified Files: DefiningaModel.tex Log Message: Modeling many-to-many relationships Index: DefiningaModel.tex =================================================================== RCS file: /cvsroot/modeling/ProjectModeling/Modeling/doc/UserGuide/DefiningaModel.tex,v retrieving revision 1.36 retrieving revision 1.37 diff -C2 -d -r1.36 -r1.37 *** DefiningaModel.tex 31 Aug 2003 17:22:50 -0000 1.36 --- DefiningaModel.tex 31 Aug 2003 17:32:25 -0000 1.37 *************** *** 1999,2009 **** --- 1999,2079 ---- \end{verbatim} + We just defined a new entity, \class{PersonAddress}, and two associations + modeling the relationships between the correlation table and the correlated + ones. + Last, we'll need to add some code in classes \class{Person} and \code{Address} + to directly manipulate the many-to-many relationships. Since relating an + object to an other one is just a matter of adding a object/a row to the + correlation table, the code is pretty straightforward. + + In \class{Person}, you'll add the following methods: + \begin{verbatim} + # Relationship: addresses + def getAddresses(self): + return self.valueForKeyPath('personAddresses.address') + + def addToAddresses(self, address): + if address in self.getAddresses(): + return + from PersonAddress import PersonAddress + _pa=PersonAddress() # Create an object in the correlation table + self.editingContext().insert(_pa) + self.addToPersonAddresses(_pa) + _pa.setPerson(self) + _pa.setAddress(address) + address.addToPersonAddresses(_pa) + + def removeFromAddresses(self, address): + _pa=[pa for pa in self.getPersonAddresses() if address == pa.getAddress()] + if not _pa: + raise ValueError, 'Cannot find address' + # Here we simply need to remove the corresponding object in the + # correlation table + _pa=_pa[0] + self.removeFromPersonAddresses(_pa) + _pa.getAddress().removeFromPersonAddresses(_pa) + _pa.setAddress(None) + _pa.setPerson(None) + self.editingContext().delete(_pa) + \end{verbatim} + And you'll add the equivalent methods in \class{Address}: + \begin{verbatim} + # Relationship: persons + def getPersons(self): + return self.valueForKeyPath('personAddresses.person') + + def addToPersons(self, person): + if person in self.getPersons(): + return + from PersonAddress import PersonAddress + _pa=PersonAddress() # Create an object in the correlation table + self.editingContext().insert(_pa) + self.addToPersonAddresses(_pa) + _pa.setPerson(person) + _pa.setAddress(self) + person.addToPersonAddresses(_pa) + def removeFromPersons(self, person): + _pa=[pa for pa in self.getPersonAddresses() if person == pa.getPerson()] + if not _pa: + raise ValueError, 'Cannot find person' + # Here we simply need to remove the corresponding object in the + # correlation table + _pa=_pa[0] + self.removeFromPersonAddresses(_pa) + _pa.getPerson().removeFromPersonAddresses(_pa) + _pa.setAddress(None) + _pa.setPerson(None) + self.editingContext().delete(_pa) + \end{verbatim} \makeatletter \def\verbatim@font{\normalsize\ttfamily} \makeatother + Now we can normally call e.g. \method{getAddresses} or \method{addToAddresses} + on a \class{Person} object, passing a \class{Address} object, without caring + about the details anymore. \subsection{How to model inheritance \label{design-inheritance}} |