JarWithLid Posted September 11, 2006 Share Posted September 11, 2006 Here is my question: Say I have the objects below. Supose an ImageSet object is created, named $fauxSet, that contains 200 images. When you call $fauxSet->getImages() you are creating an array of 200 Image objects. Awesome. So nice. But is that considered horrible as it will hit the mySql server with 201 queries when you could get the same info, without creating the objects, with just 1? The latter would have drawbacks: Sacraficing code reuse principles of OOP, no longer having the ability to use someFunction() on those objects, etc.Opinions / Suggestoins?[code]class Image { public $id; public $setid; public $name; public $description; public $file; public function __construct($id) { // Query images table, fill member variables } public function someFunction () { // Some function you can perform with this object }}class ImageSet { public $id; public $name; public $description; public function __construct($id) { // Query image_sets table, fill member variables } // This function returns an array of the Image objects in this ImageSet public function getImages () { $imgAr = array(); // What we will return $images = new mySql("SELECT id FROM images WHERE setid = $this->id"); // Gets all of the image ids from database while ($images->nextRow()) { $imgAr[] = new Image($images->id); } return $imgAr; }}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/20350-conceptual-question-about-oop-and-sql/ Share on other sites More sharing options...
btherl Posted September 11, 2006 Share Posted September 11, 2006 There's always a tradeoff between abstraction and efficiency.In this case, you can have a option or method for your class that says "I will be reading all images, read them in to a cache now". Then your getImages() can check the cache and use that data if possible, with an sql query as a fallback. It's some loss of abstraction, but you can't have everything :) Quote Link to comment https://forums.phpfreaks.com/topic/20350-conceptual-question-about-oop-and-sql/#findComment-89682 Share on other sites More sharing options...
Jenk Posted September 11, 2006 Share Posted September 11, 2006 As above, and besides creating a mySql object for all the images available surely isn't that overtly non-abstract. Instead of using a Row Data Gateway pattern, you are using a Table Data Gateway (use this instead of an array for all images)... or even Object Relational Mapping. If you still want each image to retain image specific info in it's own pattern, you should create a new object for that image upon selecting the individual image.Something like:[code]<?php$db = new DB(/* connection gubbins */);$db->exec('SELECT * FROM `images`');/* this is the table data gateway */$ImageData = new ImageDataGateWay($db);while ($image = $ImageData->fetchImage()){ echo $image->name . '<br />' . chr(10);}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/20350-conceptual-question-about-oop-and-sql/#findComment-89687 Share on other sites More sharing options...
JarWithLid Posted September 12, 2006 Author Share Posted September 12, 2006 Do you have a recomendation for a book on said design patterns?Thanks,Adam Quote Link to comment https://forums.phpfreaks.com/topic/20350-conceptual-question-about-oop-and-sql/#findComment-90161 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.