0

I’ve built a PHP ‘personal profile based’ website that has a number of ‘Category’ pages, each of which list a number of Profiles.

Category page: Lists profiles (images/names in a gallery-like layout) Profile page: Shows in-depth information about one person/profile

Im using a kinda-MVC-esque architecture here. So, when a visitor goes to a Profile page, the appropriate Controller asks the ProfileDataMapper to fetch the relevant profile. The ProfileDataMapper returns a fully formed Profile Domain Object to the Controller which then sends it to a Template View.

The domain objects are all populated from arguments to their constructors, and have validation checks in them so that if any arguments are missing or bad, they will throw an error and not be instantiated.

The problems start when I want to list a number of Profiles on a Category page. Here I am asking the DataMapper for an array of Profiles. But it is returning a list of whole complete Profile Objects when I know that in this case I only need perhaps a Name and URL. It is putting too much strain on the MySQL DB behind the mapper.

The obvious solution would be to simple make the DataMapper pull just the few fields I really need for the list. But then my Domain Model validation tests would fail due to missing data (they are expecting all fields).

So now I am thinking I should create a new Domain model called PartialProfile or ListableProfile. This domain model would just have a name/url and would only validate those two fields. The ProfileDataMapper would return PartialProfiles in its list method instead of Profiles. Maybe the Profile could even extend from PartialProfile.

Is this 'partial model' a good way to go? Or should I refactor my validation code somehow? It seems to me I need different 'levels' of validation. Sometimes I need the object with all data. Sometimes I don't.

Thanks kind people!

whoshotdk
  • 286
  • 2
  • 14
  • can you show some of your code how you are querying mysql for "DataMapper for an array of Profiles" – Viswanath Polaki Mar 27 '14 at 12:34
  • Thing is, both domain logic and persistence logic for collections is different from single instance. I would opt to have `ProfileCollection` and `ProfileCollectionMapper` for dealing with them – tereško Mar 27 '14 at 15:11

0 Answers0