Jump to content

Which of these models is better? (MVC)


enridp

Recommended Posts

Hi, I can't decide which is the best way for thinking models.

Suppose a table "users" and a table "posts".

Suppose we want to add a post, which of these models is better?

 

    class Model_User {
       public function create_post($post_data){}
    }

 

or

    class Model_Post {
       public function add_post($user, $post_data){}
    }

 

Thanks !

Link to comment
Share on other sites

Considering a User doesn't sound like it should be related to a Post probably the later. I would call the method simply 'add' too, if this class is Post we should know whats it's adding automatically.

Link to comment
Share on other sites

Yes, that was my first attempt, because I was thinking that if Model_User can create users, Post must create posts... (not users).

But after seeing some examples and discuss it with more people most of them think that the best way is that user create the post.

With this, our Model_User is not reusable (we are mixing User with Post), but if you think in terms of OO, a User has a Post, so the User is responsible for creating it, even more, in terms of "real life" the User is who creates the Post.

Then I was thinking in this solution:

 

class Model_User {
       public function create_post($data)
{
       $post = new Model_Post
       $post->add($this->id , $data)
}
}

 

Another user proposed this solution:

 

class Model_User {

    // ...

    public function post($id = NULL)
    {
        if (isset($id))
        {
            // we return an existing post
             return new Model_Post(array(
                "id"      => $id,
                "user_id" => $this->id,
            ));
        }

        return new Model_Post->set("user_id", $this->id);
    }

}

Then you can do handy things like:

 

new Model_User($user_id)
    ->post()
    ->add($this->request->post());

 

What do you think?

Link to comment
Share on other sites

A user doesn't create other Users either. A User_Manager (or similar) might.

 

But from your previous reply you said that Model_Post should create post, why Model_User can't create users then?

Do you create a new model called User_Manager to create and delete users instead of creating them with User_Model::add?

 

Link to comment
Share on other sites

When I'm doing this kind of model conceptualization I think of each of the objects as a single instance of.  My User model is a single instance of a user, that model has attributes and functionality of manipulating a single User.  If I want to manage multiple User's I create an object that would have the attributes and functions that are relevant to managing many User's.  So put against your first query about a Post object, yes I would most definitively have that as a separate model as it encapsulates the attributes and functionality of a post. 

 

Also User creating Post is a functional reasoning but we're not using a functional paradigm with the program, we're supposed to be thinking in terms of in objectifying ideas.  Its a bit of a twist in thinking, but programmers are twisted out of necessity.

Link to comment
Share on other sites

And in your design of User_Model and Users_Model, who is creating a user? both? or do you have an extra model (User_Manager) for creating and deleting users?

 

Also, I want to show you the origin of this problem:

If we are adding posts with Post_Model we have this controller:

 

$user = new Model_User($username);
$post = new Model_Post;
$post->add($user, $data);

 

and if we create the post through User_Model:

 

$user = new Model_User($username);
$user->create_post($data);

 

I think is more logical (in terms of "human") the second option, but  :shrug:

Link to comment
Share on other sites

I think is more logical (in terms of "human") the second option, but  :shrug:

Here's an example why not to do it that way:

 

$user = new Model_User($username);
$post = new Model_Post;
$post->save($user, $data);
$folder = new Model_Folder;
$folder->save($user, $data);
$image = new Model_Image;
$image->save($user, $data);
// ...

vs

$user = new Model_User($username);
$user->create_post($data);
$user->create_folder($data);
$user->create_image($data);

 

And yes, while the second option may seem "cleaner" in the example, and more "human-friendly" think of the Model_User class. Each time you want a user to add/save something new, you have to add a new method... which think about if you suddenly want to expand the user's ability to add 5 new [feature]?

 

So instead of:

class Model_User {

public function __construct($username) {
	fetch id where $username;
}

public function fooBar( ) {
	// another method in the Model_User class
}

}

 

You end up with:

class Model_User {

public function __construct($username) {
	fetch id where $username;
}

public function fooBar( ) {
	// another method in the Model_User class
}

public function create_post($post_data){
	// do post creation
}

public function create_folder($folder_data){
	// do folder creation
}

public function create_image($image_data){
	// do image creation
}

// etc...

}

Link to comment
Share on other sites

Yes, you are right about it.

But think in this situation:

You have a table Addresses, and N tables that have addresses (User, Places, Events, Photos, etc)

If you add addresses with Model_Address, then you need a lot of functions like:

 

add_to_user($id_photo, $addess);
add_to_events($id_photo, $addess);
add_to_photo($id_photo, $addess);
...

 

and why address should know about how that addresses are used?

 

if we add addresses with Model_User, Model_Event, etc we have always the same method, encapsulated in the correct model.

 

public function add_address($address){}

 

So I see benefits in both methods, and the topic is even more complex when we see thirty part examples. I've seen frequently the schema of $user->create_post();

 

Maybe is not so important, and both models are equal correct without any important avantages or disadvantages over the other.

 

Or maybe we should mix both options, when we have models like Post where the Post belongs to a User and nothing more, then Post_Model should create the posts.

But if we have a situation like Addresses, where an address can belongs to multple models, then those models should create the address and not Address_Model.

 

what do you think?

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.