开发者

Can a model house multiple functions querying different domains?

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.


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜