tags:

views:

53

answers:

2

I am building a blog based on a self-rolled MVC structure. I want to build my own, not used a 3rd party.

What is the best way to handle the creation of a new blog? Heres two options i can see...which is better, or is there something i haven't thought of?

  1. Instantiate a blank (doesn't go fetch blog info from db) Blog model, set all the values for the new blog, and then call something like $blog->create() which will do an INSERT instead of UPDATE

  2. Have a separate class just for the creation of blog which upon creation returns the new blog object.

What do you think? How do you normally go about the creation process?

+1  A: 

I'd go with 1 personally. There's no need for a separate helper class in this case; the new keyword is entirely appropriate if you're making a new entity. When it comes to getting one from the database, however, I'd use a static method (since I wouldn't be creating anything).

Having a helper class would be a clear case of over-engineering I think.

Will Vousden
+1  A: 

If you're doing MVC, you should have a controller which maps the blog input from the view into a Blog model object. You could have a helper class do this, but it probably won't be very complicated, so I'd say go with option 1 and have your controller just create the new persistent Blog object.

Kaleb Brasee
so, just instantiate a Blog "shell", set my values, and then instead of hitting $blog->save(), hit $blog->create() or something?
johnnietheblack
You could give the `save` method different behaviour depending on whether it exists in the database yet, i.e. `UPDATE` if so; `INSERT` if not. This is what I've done in the past.
Will Vousden
woudln't i have to then query the db to see if it exists, thus adding an extra query?
johnnietheblack
They way I do it is have a flag that is initially set to false, which then gets set to true either when it's saved or if it's been loaded from the database.
Will Vousden
smooth, so if the object was instantiated as an existing one, you use that query to switch the flag. thanks
johnnietheblack