Search code examples
phpmodel-view-controllerzend-frameworkzend-framework-mvc

MVC model where to put data specific checks


I'm writing my first application with Zendframework. My question is about the Model–View–Controller (MVC) architectural pattern.

I currently have a model with refer to a database table. Here's the classes that I currently have :

Model_Person 
Model_PersonMapper 
Model_DbTable_Person

Now, I see a lot of examples on the net, but all of them are simple cases of insert/update/delete. In my situation, I have to check if a person exists, and if it doesn't, I have to insert it and retrieve the ID (I know save return the Id, but it's not exactly what I have to do, this is and example).

It's quit simple, but I want to know where to put the database logic for all the others specific cases. Some others cases might involve checks across other tables or ... whatever !

Should I add all the specific functions in my Model_XXXXMapper with something that would be very specific with the current validation/process that I want to do? like a function getIdOfThePersonByNameOrInsertIfNotExists() (sample name of course!!!)

Or should it reside in the controller with some less specifics access to my model would be validated?

In other word, where do I put all the data specifics functions or check ?


Solution

  • I think the real work should occur in your model objects, not in the controller. Any selects/creates that start with the person table would be in the DbTable_Person object, things like:

    // DbTable_Person
    // returns sets of or single Person objects
    public function createByName( $name ) // perhaps throws exception if name already exists
    public function findById( $id )
    public function findByName( $name )
    public function findHavingAccount( $account_id ) // references another table
    
    // controller
    // with your example, like what Galen said,
    // I would let the controller handle this logic
    $person = $person_table->findByName($name);
    if ( !$person ) {
      $person = $person_table->createByName($name);
    }
    if ( !$person ) { throw new Zend_Exception('huh?'); }
    $id = $person->id; // you wanted the ID