I am working on an MVC application in PHP that is not using any frameworks. I am using RedBean for my ORM, which implements the datamapper pattern and works quite similiar to doctrine.
As per this question, I understand that the model is NOT the ORM object. In my project I have the following scenarios:
"Complicated" Models which need to talk with a lot of tables in the database:
- One of these models can be something like the RBAC permissions system. A controller should be able to call something like
$permission->isAllowed($controller, $action, $resource)
to determine if the user is allowed to perform the requested action. In addition, he might call$permission->getPermissions()
to get a list of permissions the user has.
- One of these models can be something like the RBAC permissions system. A controller should be able to call something like
"Simple" models where the model can generally be represented by 1 table in the database:
- One such model would be the
User
model. For example$user->changeRank()
,$user->addPoints()
and so on.
- One such model would be the
The problem I am facing now is that looking at most documentation for various frameworks, I can see that in the examples, the controller talks with the ORM directly. For example, here's an example controller from symfony2:
public function createAction()
{
$product = new Product();
$product->setName('A Foo Bar');
$product->setPrice('19.99');
$product->setDescription('Lorem ipsum dolor');
$em = $this->getDoctrine()->getEntityManager();
$em->persist($product);
$em->flush();
return new Response('Created product id '.$product->getId());
}
If an ORM is NOT the model, why is the controller allowed to interact directly with it? Shouldn't it interact with a Model that looks like this?
class ProductModel{
public function newProduct($name, $price, $description){
$product = new Product();
$product->setName('A Foo Bar');
$product->setPrice('19.99');
$product->setDescription('Lorem ipsum dolor');
$em = $this->getDoctrine()->getEntityManager();
$em->persist($product);
$em->flush();
}
}
Finally, I outlined the permissions
model earlier. Is this considered to be a model in the context of MVC? This class will be used across the whole application, as most abctions will need to check for access permissions.