blacktie524 Posted February 26, 2011 Share Posted February 26, 2011 I have an object called student which maps to a student table. There is also a courses table, and user_courses table to assign students to courses. This student object can have many course objects, and therefore, with my Students DataMapper it will retrieve student objects and any course objects for the courses that the student is enrolled in. This works fine for the cases where I need all of this information, but in the cases where I only need to display the student name and last name, the retrieval of course objects is unnecessary. My question is, how should I design my dataMappers to account for this? Should I have separate mappers for different levels of student objects, i.e. baseStudentObject with just the basic student info and then an extendedStudentObject which additionally includes the course objects? Or better to implement some sort of lazy loading solution? As always, all input is very much appreciated! Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/228866-designing-datamapper-for-objects-that-sometimes-need-to-aggregate-other-objects/ Share on other sites More sharing options...
ignace Posted February 26, 2011 Share Posted February 26, 2011 Add Lazy Loading to your Data Mapper. That way you only retrieve the course objects if you actually need them. Quote Link to comment https://forums.phpfreaks.com/topic/228866-designing-datamapper-for-objects-that-sometimes-need-to-aggregate-other-objects/#findComment-1179925 Share on other sites More sharing options...
blacktie524 Posted March 1, 2011 Author Share Posted March 1, 2011 Thx for your reply, Ignace. If there are also specific instances where you know you will need to load all of the child objects, should there be some sort of flag set indicating that all the child objects should be loaded? Or is this potential performance hit a cost of the lazy load pattern?? Quote Link to comment https://forums.phpfreaks.com/topic/228866-designing-datamapper-for-objects-that-sometimes-need-to-aggregate-other-objects/#findComment-1181582 Share on other sites More sharing options...
ignace Posted March 2, 2011 Share Posted March 2, 2011 Lazy Loading is as simple as: public function getFoo() { if(is_null($this->foo)) { $this->foo = $this->_getFoo(); } return $this->foo; } This way you will load Foo objects when you actually need them (when you call upon them) Quote Link to comment https://forums.phpfreaks.com/topic/228866-designing-datamapper-for-objects-that-sometimes-need-to-aggregate-other-objects/#findComment-1181726 Share on other sites More sharing options...
448191 Posted March 2, 2011 Share Posted March 2, 2011 Lazy Loading is as simple as: public function getFoo() { if(is_null($this->foo)) { $this->foo = $this->_getFoo(); } return $this->foo; } This way you will load Foo objects when you actually need them (when you call upon them) That is Lazy Init. Lazy Loading includes Lazy Initalization, Virtual Proxy (my personal favorite) and Value Holder. This a vproxy btw: class Meh { public function foo(){} } class MehProxy extends Meh { private $_subject; public function foo(){ if(!$this->_subject){ $this->_subject = $this->_getSubject(); //From wherever } return $this->_subject->foo(); } } Quote Link to comment https://forums.phpfreaks.com/topic/228866-designing-datamapper-for-objects-that-sometimes-need-to-aggregate-other-objects/#findComment-1181840 Share on other sites More sharing options...
blacktie524 Posted March 5, 2011 Author Share Posted March 5, 2011 Appreciate the input guys! My followup question is that if you know that you will need to load all child objects in a specific situation, is there a way to load them all at once, rather than have them automatically set to lazy load, and therefore instead of x amounts of queries to the db, it would just be 1 for this scenario? Quote Link to comment https://forums.phpfreaks.com/topic/228866-designing-datamapper-for-objects-that-sometimes-need-to-aggregate-other-objects/#findComment-1183304 Share on other sites More sharing options...
ignace Posted March 5, 2011 Share Posted March 5, 2011 Who say's you can't load them all at once using Lazy Loading? The technique just delay's the read to the DB until you actually need them. Quote Link to comment https://forums.phpfreaks.com/topic/228866-designing-datamapper-for-objects-that-sometimes-need-to-aggregate-other-objects/#findComment-1183307 Share on other sites More sharing options...
blacktie524 Posted March 5, 2011 Author Share Posted March 5, 2011 Wouldn't that mean that if I had 10 Parent Objects with 1 Child Object each, it would lead to 10 read's to the DB for the child objects, instead of somehow setting it up to have just 1 read to obtain all the child objects? Quote Link to comment https://forums.phpfreaks.com/topic/228866-designing-datamapper-for-objects-that-sometimes-need-to-aggregate-other-objects/#findComment-1183308 Share on other sites More sharing options...
ignace Posted March 6, 2011 Share Posted March 6, 2011 Yes, if each parent object was a stand-alone class. But you can create a class that holds all parent objects and upon retrieving a parent from the object it would load all applicable child objects and populate the parent object. class FooRepository { public function getBar($i, $loadChildren = true) { if(!array_key_exists($i, $this->bar)) { $this->bar[$i] = $this->_loadBar($i, $loadChildren); } if($loadChildren && !$this->bar[$i]->hasChildren() && $this->bar[$i]->hasFuckBuddy()) { // Found that in "God's Stuff/Earth/mating.source" $this->bar[$i]->setChildren($this->_getChildren($i)); } return $this->bar[$i]; } } Quote Link to comment https://forums.phpfreaks.com/topic/228866-designing-datamapper-for-objects-that-sometimes-need-to-aggregate-other-objects/#findComment-1183459 Share on other sites More sharing options...
448191 Posted March 6, 2011 Share Posted March 6, 2011 With Virtual Proxy the object is a transparent replacement for the actual object. So you issue a query to the database, if the resulting data contains the data needed to fully load the related object, you just construct and set the normal object. If you did not load the related data (but did for example load a foreign key so you can load later), you set the vproxy instead. Client code is completely unaware of the difference: they cannot tell the diff between a "Meh" and a "MehProxy". This solution is far superior for the purpose of ORM to other lazy init solutions. Quote Link to comment https://forums.phpfreaks.com/topic/228866-designing-datamapper-for-objects-that-sometimes-need-to-aggregate-other-objects/#findComment-1183566 Share on other sites More sharing options...
blacktie524 Posted March 6, 2011 Author Share Posted March 6, 2011 Thx guys, it's a lot clearer to me now! Quote Link to comment https://forums.phpfreaks.com/topic/228866-designing-datamapper-for-objects-that-sometimes-need-to-aggregate-other-objects/#findComment-1183762 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.