Hi,
Hector Virgen wrote: > > The example in the Zend Framework Quick Start [1] involves only a single > model and a single mapper, which is fine -- the pattern works beautifully > in > isolation. But in my application I have many models each with their own > mappers, and the models are related to each in one-to-one, one-to-many, > and > many-to-many relationships. > I think the data mapper pattern is not implemented right in the QuickStart guide which leads to series of questions like yours. Even on the http://martinfowler.com/eaaCatalog/dataMapper.html reference page we see: > A layer of Mappers (473) that moves data between objects and a database > while keeping them independent of each other and the mapper itself. > According to this statement the class Quiz may not depend on any Data Mapper, including the QuestionMapper. The client code (e.g. action controller) using a Data Mapper may look like this: //fetching questions for some quiz $questions = $questionMapper->findByQuiz($quiz); $questions is now a collection of objects with complete question informaton including the answer choices. The client doesn't ask the Quiz Entity to get the questions, but the mapper. The question mapper may communicate with the AnswerChoice mapper to fetch the choices and populate the questions with them. Implementing it this way you make your Model independent of the Mapper and therefore the storage type. Hector Virgen wrote: > > Also, since each question belongs to a quiz, should the Question object > contain an instance of the Quiz object it belongs to? > I think it's acceptable if you use this backreference somewhere. Hector Virgen wrote: > > If I follow this pattern, then when I "find" a single AnswerChoice object, > the AnswerChoiceMapper would load the parent Question object, which loads > the parent Quiz object, which loads the parent User object, etc. This > seems > to be inefficient, especially when iterating through multiple answer > choices. > With the Data Mapper layer decoupled from your model you may now implement another mapper for each use case which pulls exactly as many dependencies as needed. You could still use techniques you mentioned like Identity Map or Lazy Loading. Martin Fowler describes them very good in http://martinfowler.com/books.html#eaa PoEAA . Tim. -- View this message in context: http://www.nabble.com/Data-Mappers-and-Relational-Modelling-tp25193848p25198001.html Sent from the Zend Framework mailing list archive at Nabble.com.