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
}
}