doupaoyan6083 2010-07-23 20:00
浏览 30
已采纳

模型可以容纳多个查询不同域的函数吗?

I have a domain for users that connects to my user table, which includes information like username, first name, and last name.

Then I have a domain for emails, that connects to the email table because a user can have more than one email. The email table consists of a fk to connect the user with their emails, and other fields like address, status, etc.

Should I have different domains for the separate tables, but combine functions, that call the domains, in the models? Or maybe you can but it's not best practice.

I am new to this MVC thing and it's hurting my brain right now. Maybe, it just someone hasn't explained it well enough.

  • 写回答

1条回答 默认 最新

  • dougou6727 2010-08-12 03:07
    关注

    Question: when you mention having a "Domain", are you referring to a User model, and an Email model? or to the design pattern?

    Also, initially (but depends on your application) having the email database logic inside the users model is more logical (to me), since I don't think you are going to add email addresses without creating a user. That is, the email model really depends on the user model, and perhaps only on the user model, so maybe they should be combined?

    The way I would do it:

    1. Put all the database logic inside the models which I assume is the way you have done it.

    2. Create a library or class to place the business logic of the application that concerns users. (For example, uploading an image, or connecting to a web service, should not be in the model if you follow the standard that CodeIgniter models should only contain database logic, so that's why I create another class to handle those cases)

    3. Now, when you want to create an user, you just inject the models in the library (you could do this at the controller level), and call your abstract method create_user()

    class Users extends Controller {
        public function create() {
            // these could be in the constructor!
            $this->load->model('users');
            $this->load->library('users_logic');
            $this->users_logic->set_model($this->users);
            // and/or: $this->users_logic->set_email_model($this->email_model);
    
            if ($this->input->post('name')) {
                $this->users_logic->create();
            }
        }
    }
    

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊