4

Can I do this in a Controller:

$this->User->read(null, $id);
$this->User->find('list');

Is it correct?

Am I using MVC correctly?

Can these easy functions be used in a Controller? Or, do I need to create these functions in the Model? Like Model->getUser(), and have that function use Model->read().

I know that functions it's called by Model, but, when I want pass some parameters, and function makes big, for example:

$this->User->find('all', array(
    'conditions' => array(
        'User.active' => true,
        'User.group_id' => 3,
        'User.age >=' => 18
    )
));

Can I call this function in Controller, or need create a custom function in Model, to call it? Like... $this->User->findSomeCustomFunction($param1, $param2, $param3)?

11
  • 2
    To correctly use MVC , you should be getting rid of CakePHP. It does not implement MVC design pattern. Active record instances are not "models", templates are not "views" and controllers are only responsible for changing the state of model layer. Commented Mar 13, 2013 at 18:35
  • I'm with @tereško on this; but, for that matter, I don't really understand how any back-end blocking frameworks implement the MVC pattern. For example, I'm a Django fan, which is alleged to be an MVC framework. However, it's difficult to discern the MVC components in Django, despite there being "models" and "views". I've never used CakePHP in any real capacity, but in my limited exposure I've also been puzzled by the MVC designation. With Cocoa/iOS development, the MVC is clear as day. Views delegate to controllers, which in turn work with models to update views. Commented Mar 13, 2013 at 18:51
  • @tereško Ya... If Cake is or not a MVC framework, I don't know (for me, it is). But for now... although not a 'mvc framework' which would be correct? Commented Mar 13, 2013 at 19:28
  • 1
    Don't put find()s in your Controller. You're just going to clutter your code, have many many more changes to break the DRY concept, and make programming in a team nightmarish. - my 2 cents. Commented Mar 13, 2013 at 20:25
  • 1
    @PatrickMaciel - Np! glad I could convince you :) I really think you'll be more happy w/ maintaining your code in the long run. Commented Mar 13, 2013 at 20:31

2 Answers 2

4

TLDR:

It's "ok" to call a find() from your Controller, however best practice is to put any/all find()s in your models.

If you make a habit of putting all your find()s in your models, it will make it much easier to maintain your code in the long run.

Explanation/example:

In this case, as an example, you could start with a seemingly simple function:

//User model
public function getUsers() {
    return $this->find('list');
}

But later, maybe you need something more along the lines of:

//User model
public function getUsers($opts = array()) {
    $defaults = array(
        'findType' => 'all',
        'activeOnly' => true,
    );
    $params = array_merge($defaults, $opts);
    $qOpts = array('conditions' => array());        

    //active only
    if(!empty($params['activeOnly'])) $conditions[$this->alias.'.active'] = 1;

    return $this->find($params['findType'], $qOpts);
}

(Pardon if there are many ways to make that code better - it was just off the top of my head - It gives you the idea.)

Keeping all your find()s in the Model also keeps you from having to search through each Controller every time you want to write a find() to determine if you've used a similar find() anywhere else. If you're programming as a team, that can be a nightmare, and you're almost guaranteed to be duplicating code.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice tips, every one appreciate.
3

It is perfectly fine to call Model->find() from a Controller. However, you will also want follow the DRY (Don't Repeat Yourself) principles. That basically means "Don't copy-paste code everywhere."

So, if you find that you need to make this exact Model->find() call from many Controller actions, it is considered good practice to abstract it into a function call against the Model. So yes, your Controllers would then call $this->User->findSomeCustomFunction().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.