开发者

Model class for all database table

开发者 https://www.devze.com 2023-02-02 03:21 出处:网络
Is it necessary to have a model class for eac开发者_如何学Pythonh database table in CakePHP? And is it necessary to have one controller for each model class?

Is it necessary to have a model class for eac开发者_如何学Pythonh database table in CakePHP?

And is it necessary to have one controller for each model class?


Is it necessary to have model class for each database table in cakephp?

Generally, yes. If you wish to retrieve, modify or delete data from a particular table, then the CakePHP way to do it would be to create a model for that particular table and use the built-in methods. Exceptions would be:

  • Join tables: If the join tables are very simple, you don't need to create a model for them.
  • If for some reason you didn't want to create a model for a few tables, and used the query function of another model to access them.

On the contrary: You can have models without tables which can be very useful to validate forms. I never find myself have less number of models than the number of tables I have (minus the simple join tables).

Is it necessary to have one controller for each model class?

No, it's not. While MVC patterns dictate that each model should be manipulated by its own controller, there are times when a single controller handling more than one model makes the code simpler. An example would be having a User model, which hasOne Profile and hasMany Address. You could manipulate all 3 models from the UsersController:

class UsersController extends AppController {
    var $uses = array('User', 'Profile', 'Address');

    function edit_profile() {
        ...
    }

    function change_address() {
        ...
    }
}

Always use MVC as a guiding principle. If you deviate too much from the architecture, things will easily fall apart. However, in the end, clean, organized and working code is what matters. :)

HTH.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号