I got a problem concerning the possible hierarchy of models in a MVC-pattern.
Consider the开发者_开发问答 following situation:
The database have three tables. Gallery Album Image
The gallery has 0..* albums The album has 0..* images
I also got one model for each table, hence three models. The question is; should I got with a flat structure or a deep structure.
If I want to rename an image we got the following to code snippets.
Deep structure were the hierarchy between models are taken into consideration.
public function RenameImage($intAlbumId, $intImageId, $strNewName)
{
$objGallery = new Gallery();
$objGallery ->LoadByInput($intPackageId);
$objAlbum = $objGallery->GetAlbum($intAlbumId);
$objImage = $objAlbum->GetImage($intImageId);
$objImage ->SetName($strNewName);
$objImage ->Save();
}
Or the flat structure:
public function RenameImage($intImageId, $strNewName)
{
$objImage = new Image();
$objImage ->LoadById($intImageId);
$objImage ->SetName($strNewName);
$objImage ->Save();
}
The deep structure will create more code but also more dependencies and a better security were you cant access a model without going through the parent model.
The flat structure will create less code but also lack on the security part.
How would you do it?
精彩评论