I am working on saving state of an object to a database using the data mapper pattern, but I am looking for suggestions/guidance on the validation and error message generation step (step 4 below). Here are the general steps as I see them for doing this:

(1) The data mapper is used to get current info (assoc array) about the object

in db:

|  person_id  |   name   |   favorite_color   |  age  |  
|   1         |   Andy   |   Green            |  24   |   

mapper returns associative array, eg. Person_Mapper::getPersonById($id) :

$person_row = array(
    'person_id' => 1,
    'name' => 'Andy',
    'favorite_color' => 'Green',
    'age' => '24',

(2) the Person object constructor takes this array as an argument, populating its fields.

class Person {

    protected $person_id;
    protected $name;
    protected $favorite_color;
    protected $age;

    function __construct(array $person_row)
        $this->person_id = $person_row['person_id'];
        $this->name = $person_row['name'];
        $this->favorite_color = $person_row['favorite_color'];
        $this->age = $person_row['age'];

    // getters and setters...

    public function toArray()
        return array(
            'person_id' => $this->person_id,
            'name' => $this->name,
            'favorite_color' => $this->favorite_color,
            'age' => $this->age,

(3a) (GET request) Inputs of an HTML form that is used to change info about the person is populated using Person::getters

    <input type="text" name="name" value="<?=$person->getName()?>" />
    <input type="text" name="favorite_color" value="<?=$person->getFavColor()?>" />
    <input type="text" name="age" value="<?=$person->getAge()?>" />

(3b) (POST request) Person object is altered with the POST data using Person::setters


*(4) Validation and error message generation on a per-field basis
- Should this take place in the person object or the person mapper object?
- Should data be validated BEFORE being placed into fields of the person object?

(5) Data mapper saves the person object (updates row in the database):

// the savePerson method uses $person->toArray() 
// to get data in a more digestible format for the 
// db gateway used by person_mapper

Any guidance, suggestions, criticism, or name-calling would be greatly appreciated.

  • This is a very good question, +1. However it is not really a code review so I am voting to close. I think this will be a better match for another SE site.
    – Paul
    Oct 13 '12 at 8:01
  • @Paul might you suggest an SE site to migrate to? Oct 14 '12 at 15:18
  • @AndyPerlitch It would be on topic on Programmers, it's a whiteboard problem. Already flagged and asked the mods to send it over, please do the same (as you are the OP).
    – yannis
    Oct 14 '12 at 18:53
  • Thanks @Paul and Yannis Rizos. Didn't see this in time to flag it but I suppose it didn't matter after all! Oct 15 '12 at 15:07

I think you should define in the person object — and not in the mapping object — the validation logic.

You get the POST data and assign it to the person object, and if there is any error, it should happen there, before you reach the mappign step. In fact, you should never call savePerson() when the data provided is not correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.