0

i'm wondering which is the best way to retrieve an object that contains data from two tables. In my case I want to retrieve a portfolio item which has data in the portfolioItem table and a separate table with the images.

Now I'm struggling for which is the best way to retrieve these items in an object. In the first way I retrieve everything at once in my Data class and return the complete object while the second way builds the object from two different data classes (one for the general portfolio information and one for the images).

Now I'm just wondering, is there a way which is beter than the other or are both ways acceptable? Is it ok to get all the data at once or should I break it up pieces like the second way?

I have two tables: portfolioItem and portfolioItemImage I have a class like

PortfolioItem{
    public $portfolioItemId;
    public $name;
    public $images //array
}

1:

$dataPortfolioItem = new DataPortfolioItem();
$theItem = $dataPortfolioItem->getPortfolioItem(5);
DataPortfolioItem{
    public function getPortfolioItem($theId){
        //create a new portfolio item
        //get the portfolio data from portfolioItem table and fill in the id and name
        //get the image data from image table and set the images array from the object
        //return the object with the data and images included
    }
}

2:

$dataPortfolioItem = new DataPortfolioItem();
$theItem = $dataPortfolioItem->getPortfolioItem(5);
$dataPortfolioItemImages = new DataPortfolioItemImages();
$theItem->images = $dataPortfolioItemImages->getPortfolioItemImages(5)

DataPortfolioItem{
    public function getPortfolioItem($theId){
        //create a new portfolio item
        //get the portfolio data from portfolioItem table and fill in the id and name
        //return the portfolio item     
    }
}

DataPortfolioItemImage{
    public function getPortfolioItemImages($theId){
        //get the images from the database table and return them in an array
    }
}
randomizer
  • 1,614
  • 3
  • 14
  • 31

1 Answers1

1

"In my case I want to retrieve a portfolio item ...". If that is the case, the your Data Access Layer (DAL) should return Portfolio objects, as opposed to returning pieces of it in two separate objects.

A couple of general things regarding the design. Your application logic should be based on your model, should not know about the data storage details. If that is the case, then your DAL should only provide Model objects.

Aside: "way builds the object from two different Data layers, one for the portfolioItem data and one for the images." By layer layer you probably mean tables. Layer is something different.

Nazar Merza
  • 3,037
  • 1
  • 17
  • 19
  • Ok, thanks, I also edited the answer, I meant two different data classes to build the object, two data layers was indeed not correct. – randomizer Nov 06 '12 at 08:31