MVC and Database interface questions

Hello all,

I went through the entire MVC sticky thread and came up with a few questions. Several responses in the thread posted similar questions, but there were not clear answers.

  1. There was some discussion about some pages not needing authentication and yet one of the filters in the filter chain handles authentication. How do we tell this filter not to processRequest()? There wasn’t a clear solution presented for this problem. Do we rebuild the chain every time the FrontController runs? If so, we would have to examine the request to determine what page we are on before filtering. This seems to break the architecture. Some also argued that authentication shouldn’t take place in a filter.
  2. What is the best practice for storing the logged in user? I would assume a variable $loggedInUser. However, some questions come up:
    [LIST=2]
  3. Where is the variable stored? In the FrontController (if using one)? In the auth filter mentioned above? As a global? Somewhere else?
  4. Is this variable a reference to a User Model? Or a reference to a lighter weight User class, that just has id, username, admin_state?
    [/LIST]
  5. I’m not sure I completely grasp the idea of the filters. I think I get the idea of the authentication filter, but what else would be implemented as a filter? They mention using filters for pre- and post-processing steps. What exactly do they mean there? Validating $_GET, $_POST and $_REQUEST parameters? Could someone please provide some clarity?
  6. What are the best practices for Model design? Should the interface to a Model just contain the basic CRUD methods? CakePhp, for example, puts the CRUD methods in the Model’s corresponding Controller. But when examining the description of MVC, to me it seems that they belong in the Model. In the Wikipedia article for MVC they state the following: “The controller receives input and initiates a response by making calls on model objects.”
    When I read that, I visualize the following example:
    User clicks the delete link for a product on a web page.
    The controller receives this request and executes it like so:
    $model = Models::getModel(‘product’);
    $model->delete($id);

    But in CakePHP it would be:
    productController->delete($id);
    :confused: Perhaps CakePHP uses a slightly modified implementation of the MVC pattern??

And now my database interface questions.
Again I’m searching for best practices. I’m in the initial phases of coming up with a design for a database interface design and have come up with the following interface:

interface MyDatabaseInterface{

function addItem($new_item);
function getItem(id);
function deleteItem(id);
function updateItem($updated_item);
}

Ok, fine and dandy. A concrete implementation of this may be a MySQL implementation that knows how to create MySQL queries and perform the above actions. However, the above interface is a bit limited. Say I wanted to delete all items where name=“Fred” (or search for items where name = fred). I could add an interface methods called:
function deleteItem($conditional);
function getItem($conditional);

The MySQL implementation would construct a query like "DELETE (or SELECT) from table WHERE name like ‘%fred%’;

Fine and dandy. But how should the “$conditional” parameter be designed. I once saw a solution to a similar problem, where they passed in a “Where” clause as a parameter.
So the function would be:
function deleteItem($whereClause);
The $whereClause may make sense for MySQL or SQL, but not for other databases. If I used that in my database interface above, I would be tying specifics of MySQL to the database interface, which should be agnostic of all concrete implementations.
So, what is the recommended way for designing such interfaces?

Thank you for reading and I look forward to reading your thoughts on the above.

Perhaps CakePHP uses a slightly modified implementation of the MVC pattern??

CakePHP actually uses something closer to MVP (Model-View-Presenter) even if it calls its presenter “controller”.

As for your questions:

1,3: there’s no such thing as a “filter” defined in MVC, what are you actually trying to achieve?

2: The model. All states should be stored in models. Generally in MVC the model is split into two parts, the Application Model and the Domain Model (see http://www.jdl.co.uk/briefings/mvc.html ) the “Logged in user” would be in the application model while the actual model of the user is the domain model.

4: You’re correct, CakePHP is wrong. These belong in the model. You will find This article helpful.

Sorry I took the link from my bookmarks. Looks like the article URL has changed: http://blog.astrumfutura.com/2008/12/the-m-in-mvc-why-models-are-misunderstood-and-unappreciated/

Thank you for the links TomB.
The 2nd link actually lead me to a good MVC article which cleared up some confusion:

http://www.survivethedeepend.com/zendframeworkbook/en/1.0/the.architecture.of.zend.framework.applications

I’ll soon be reading the new one you just posted and the first.

I’m not exactly sure. :frowning:
In a lot of the articles I have read and even here they describe the Intercepting Filter Pattern. I’m trying to understand it’s use in MVC. What specific kind of things would I need it for?

For example, in most of the Intercepting Filter examples, an authentication filter is mentioned. This injects confusion into my understanding as I don’t see why this is being presented as a filter.
Shouldn’t logging in have it’s own MVC triad? With (1) a log in View, that renders a form with a username field, password field and submit button.
(2) the use of the User Model
and (3) a controller to coordinate the process ???
So why is this all being done in a Filter ??? :confused:

Well going by this definition: http://java.sun.com/blueprints/corej2eepatterns/Patterns/InterceptingFilter.html

There are many ways to handle this. The basic logic is “Should the requested traid be initialised or a different one?”. This would be handled in the front controller.

For example, in most of the Intercepting Filter examples, an authentication filter is mentioned. This injects confusion into my understanding as I don’t see why this is being presented as a filter.
Shouldn’t logging in have it’s own MVC triad? With (1) a log in View, that renders a form with a username field, password field and submit button.
(2) the use of the User Model
and (3) a controller to coordinate the process ???
So why is this all being done in a Filter ??? :confused:

You’re right that it’s its own triad. The filter, as I understand it, would just cause the request to initiate the login triad rather than the requested one.

edit: Personally, I’d suggest just marking controllers as requiring auth, e.g.


class MyContentController extends Controller implements AuthRequired {
}

as this gives you a degree of flexibility without the need to store somewhere metadata about which controllers have filters.