1

I am running into a problem to validate a model which refers to other models.

I have an 'user' model and a 'profile' model. I want to create the profile but I need to check if the user actually exists.

In my profile model I have a method 'validateUser' but I would either have to write a query to a specific table within a database or I have to create a user model object and call exists(id).

Both options seem to have a lot of drawbacks. The table name could change I would have to go over all models that use it. With the user mode object I would have to create an object within the profile model or inject it.

What is the best way to approach this?

7
  • There are no "models". Model is a layer, not a class or instance. What are you actually referring to? Are you talking about active record instances or domain objects?
    – tereško
    Sep 11, 2013 at 19:23
  • I am referring to domain object that is an active record. I assume I am not following law of demeter?
    – John
    Sep 11, 2013 at 19:27
  • You cannot have both. Domain object contains only business logic. Active record also interacts with storage (and thus, violates SRP). And, if you are using active record, I am not even sure that this problem can be solved in a sane manner.
    – tereško
    Sep 11, 2013 at 19:29
  • If I understand correctly the domain object only contains the properties and called upon methods? and the active record has all the validation and queries to the required storage?
    – John
    Sep 11, 2013 at 19:32
  • I linked you to articles that briefly explained each term.
    – tereško
    Sep 11, 2013 at 19:33

1 Answer 1

1

The domain of doom ..

In real world situation the validation is rarely a straightforward process. Instead you have several unrelated concerns:

  • checking if data inside an instance is consistent with business rules
  • making sure that state of instance is not conflicting with other domain structures
  • verifying the integrity of information against existing data and constraints in storage

If you are using active record for representing your domain model (don't confuse with M in MVC), then all these three aspect become a responsibility of single object.

Which is what you have now.

But there is hope ..

The best option is to separate all these responsibilities (all hail SRP). Basically what you do is divide your current setup in thee different groups of structures:

  • domain objects: for dealing with specific rules of domain entity

  • data mappers: for storage abstraction

  • services: for interaction between domain objects and mappers (or other domain objects)

Since your question was somewhat confusing (the were users and profiles and saving and validation and then something wasn't there), I am not sure if I understood it correctly, but here is a small example:

public function createProfile( $id, $email, $name )
{
    $account = new Account;
    $account->setId( $id );

    $accountMapper = new AccountMapper( $pdo ); // explained below

    if ( $accountMapper->fetch( $account ) === false )
    {
        $this->errors[] = .. something about missing account
        return;
    }

    $profile = new Profile;
    $profile->setEmail( $email )
            ->setName( $name );

    if ( $profile->isValid() === false )
    {
        $this->errors[] = .. something about invalid profile
        return;
    }
    
    try
    {
        $profileMapper = new ProfileMapper( $pdo ); // explained below
        $profileMapper->store( $profile );
    }
    catch ( Exception $e )
    {
        $this->errrors[] = .. something about failing to create profile
        return;
    }

    $account->addProfile( $profile );
    $accountMapper->store( $account );
}

This is extremely simplified example. Especially where mappers are initialized, because that part in real-world situation would be handles by some factory. Kinda like described in this post.

The point here is that the validation of domain data and and insurance of DB integrity is done separately. The underlaying APIs for interaction with database actually return you error code is you violate UNIQUE KEY or FOREIGN KEY or any other constraint, which you can then use to determine what went wrong.

The method itself would be part of a service (in this case - some service that manages user accounts).

Note: if your application needs to perform multiple SQL interactions per operation and those interaction need to be done as transaction with ability to do rollback, then, instead of using data mappers directly, you should be looking at implementing Units of Work. To learn about UoW, you will have to read Patterns of Enterprise Application Architecture, because it's really extensive subject.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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