1

A while ago I created my own MVC to learn about this pattern and to do some practice. Now, I want to start using an orm in my MVC. However, I'm not quite sure how to accomplish this.

As for now, the only thing my controllers do is pass and retrieve data and that's it. Performing crud operations is the job of the models.

$controller->$model->getUsers();

However, when looking into some documentation at for example Symfony, I see they directly communicate with the ORM (Doctrine) in the controller.

public function createAction()
    {
        $job = new Job();
        $job->setPosition('Web developer');
        // .. set other fields

        // get the entity manager
        $em = $this->get('doctrine.orm.entity_manager');

        // persist the object to database
        $em->persist($job);
        $em->flush();

        // ...
    }

Also for CodeIgniter a tutorial shows that is has been used in the Controller: http://talkweb.eu/how-to-run-doctrine-orm-with-codeigniter/

Why is that? Now you are doing a lot in your controller that should not be there at all I think, why not create a function in the model to call in your controller like

$this->model->addJob("web developer"); or something...

Dani Sancas
  • 1,365
  • 11
  • 27
randomizer
  • 1,619
  • 3
  • 15
  • 31
  • 6
    Because those tutorials have no relation to MVC or MVC-inspired architectural patterns. They are focusing on fast prototyping instead. – tereško Dec 25 '13 at 19:24
  • So I am correct it is bad practice and communication with the ORM should happen in the model not in the controller? – randomizer Dec 25 '13 at 19:37
  • Kinda, yes. Though, you seem to be under impression that "model" is some class or object. Instead MVC (and actually other MVC-like architectures) is made up from two layers: presentation layer and model layer. Controllers are part of presentation layer. ORMs would be part of model layer - in particular - part of persistence abstraction. – tereško Dec 25 '13 at 20:16
  • But the model is always a class that corresponds with your controller? Like UserController and UserModel where UserModel holds functions like getUser, getUsers which communicate with the database and pass back data to the controller? –  Dec 25 '13 at 20:27
  • @BartL. soft answer: no – tereško Dec 25 '13 at 20:28
  • @BartL. long answer: [noooooooooooooooooooooooooooooooooooooou](http://stackoverflow.com/a/5864000/727208) – tereško Dec 25 '13 at 20:29
  • I don't understand what you mean then. http://www.cs.cmu.edu/~pattis/15-1XX/15-200/lectures/modelinmvc/ "Model: This class is the bridge between the control and the view" –  Dec 25 '13 at 20:30
  • LOL. Well ... the explanation of how information flows in (desktop) MVC-like applications is quite correct. It's just the scope is wrong. The author there gives a simplified example in order to teach. In real world, MVC architecture is applied to the application to introduce additional constraints, because code-base has become too complicate to handle said complexity only using OOP best practices. What people usually refer to as "models" are actually [domain objects](http://c2.com/cgi/wiki?DomainObject). – tereško Dec 25 '13 at 20:39
  • But in most php frameworks like codeigniter a controller communicates with a corresponding model class which performs crud operations, is that incorrect then? These are indeed models, no? –  Dec 25 '13 at 20:43
  • 1
    CodeIgniter has nothing to do with MVC. It just uses the naming conventions. It has "logic" class which passes data from active record (or some other database abstraction) to a template. – tereško Dec 25 '13 at 20:44
  • OK but most mvc frameworks do have a model class to handle db stuff no? – randomizer Dec 25 '13 at 20:55
  • Symfony 2 is a request/response framework. It would be a mistake to think of it as an MVC framework. Many Symfony 2 examples do put a bunch of code in the controller. Code which might perhaps be best put in independent services. Fcous on what works best for your applications and not on trying to follow some sort of patten. – Cerad Dec 25 '13 at 21:43
  • Ok thx for all the info guys, I will look into what best fits my needs. – randomizer Dec 26 '13 at 08:36

0 Answers0