Should I completely separate models and ORM in MVC?

I am working on an MVC application in PHP that does not use any frameworks. I use RedBean for my ORM, which implements the datamapper pattern and works exactly the same as doctrine .

According to this question, I understand that the model is NOT an ORM object. In my project, I have the following scripts:

  • "Complex" models that need to talk to a large number of tables in the database:

    • One of these models may be something like an RBAC permission system. The controller should be able to call something like this $permission->isAllowed($controller, $action, $resource)to determine if the user is allowed to perform the requested action. In addition, it can call $permission->getPermissions()to get a list of user permissions.
  • "Simple" models, where the model can usually be represented by 1 table in the database:

    • One such model will be a model User. For example $user->changeRank(), $user->addPoints()etc.

The problem that I am facing now is that, looking at most of the documents for various frameworks, I see that in the examples the controller directly communicates with ORM. For example, here is 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());
}

ORM , ? , ?

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();
   }
}

, permissions. MVC? , .

+5
1

ORM (Object Relational Mapper) . (). , ORM-, ( ), , , .

ORM , . . , actAs: { Timestampable ~} actAs: NestedSet: hasManyRoots: true. , , , (.. 1: M, M: M refClass ..)

, . - , . , ( ) , ORM.

, , , - ( ) . , . , ( ), "" ( "" ). . -, .

- , , "" , . , -.

---

, API . , , , , MVC, API .

+2

All Articles