6

We're working on a fairly code-heavy PHP5 project, and in the last week I worked on a PoC of a RESTful API. We are separating Model Classes from Business Classes.

Trying to implement CRUD functionality, I discovered, that implementing CRUD against the Models directly would be pretty straight-forward while implementing it against the Business Logic isn't, since its functionality is specific to the currently existing Views and its interface doesn't provide the general data access model I need for implementing the API.

Thinking about this, the following questions came to my mind:

  • What is the best way to interact with the data, keeping the flexibility of the model and keeping the functionality the model doesn't care about at the moment (like sending a mail with an activation link when changing the email address)?

  • Working a lot with django previously, where most of the business logic is implemented in the model, why should you keep the business logic separate anyway? Do you have any real-life examples? What problems does this solve, if any?

Some possible solutions came to my mind, too:

  • Putting the whole business logic into the model. Checking which fields changed after / before calling the save method.
  • Using the observer pattern to notify the business objects of changes in the model and interact with the models directly.

What are the pros and cons in your opinion, what would you do?

1
  • In MVC, the Model is not the database (or some other persistence). The Model in MVC is where significant algorithms for solving the business problem live. It just so happens the Model typically communicates with the database usually @Luc Franken writes via a Data Access Layer (i.e. Gateway, Data Access, Active Record and/or Data Transfer objects).
    – orangepips
    Commented Jul 22, 2012 at 16:50

1 Answer 1

2

If you separate out your storage in data a data access layer you get the separation you want and the functionality in the model:

DAL (doing queries etc)
  |
Model (doing business)
  |
Controller
  |
View

So in the view you have an action: Mark as Paid. So you controller gets the request (POST) /invoice/1/markaspaid (or any other url structure you use). Then the controller calls the model:

$Invoice=new Invoice();
$Invoice->markAsPaid(1);

Then your model calls the DAL to actually store this change. There is no real need to separate this from the models. If it is very complex or very transactional you might think about a separate service for a complex task. That way your model gets thinner and the complex part gets separated.

What is the best way to interact with the data, keeping the flexibility of the model and keeping the functionality the model doesn't care about at the moment (like sending a mail with an activation link when changing the email address)?

I don't understand this totally. As far as I see it you should separate the send e-mail process anyway from your normal code run. So put it in a queue and find it out there. It isn't part of your normal code path. You might initiate it from your model but that's about it.

See this question which has relevant info on that topic: Cakephp cron job to call a controller's action

3
  • 1
    Thanks for the answer! We are already separating the send e-mail process. What I mean is, what is the best way to change the data directly (like $user->email = "[email protected]";), without loosing the possibility to do additional tasks that have to be done when a certain value changes.
    – stefreak
    Commented Jul 21, 2012 at 11:52
  • I hope you understand me now, I'm sorry if my english makes me hard to understand ;)
    – stefreak
    Commented Jul 21, 2012 at 12:06
  • Do you really want to write hard to the model? How should it be saved then? I would always call a method like $Invoice->save(); so you really decide when you data get's updated in the datastore. Commented Jul 21, 2012 at 12:12

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