Welcome Guest, Not a member yet? Register   Sign In
Model questions
#1

[eluser]robmcm[/eluser]
Hi,

I have a system similar to an online store.

Currently I have a model for each table in my database, such as order, item, address, user.

I want to create a model for a total order, that has the user, their order, the item and their address (this is theoretical, so please don't ask why Wink ).

Ideally I would create a model for order, that loaded in related tables. Does this sound normal, and does anyone have any examples.

Also I have noticed that most example models return the SQL result set, doesn't it make more sense to return an instance(s) of the model(s) its self, that is built from the query results?

I don't want to break the use of scaffolding, by changing the default methods (although I guess I could create new ones Smile )

Thanks for you help

Rob
#2

[eluser]Colin Williams[/eluser]
Quote:Also I have noticed that most example models return the SQL result set, doesn’t it make more sense to return an instance(s) of the model(s) its self, that is built from the query results?

Your model should typically return objects (or arrays of objects) that represent data that the application needs. Sometimes a query result is enough to represent the object, sometimes it is not (like when relations are involved.) Before you even write your application, it's a good idea to prototype the objects you plan to control in your app. Then you can write your app (models especially) around this prototype. And model != SQL query monkey. Sometimes models will query and parse files or directories, or call external services and APIs.
#3

[eluser]robmcm[/eluser]
Thanks Colin,

I thought it was funny that the examples return a row rather than the object as you then can't update the model unless manually set the model with your row...?

Perhaps they should update the model example Wink

Rob
#4

[eluser]Colin Williams[/eluser]
Well, the row might be a proper representation of the object.

Quote:you then can’t update the model unless manually set the model with your row

Well, that's only true if your model serves as a representation of a single object. That typically isn't true, but you certainly can construct your model that way.
#5

[eluser]robmcm[/eluser]
Thanks again.

So would you suggest that a model gets a row, then sets all it's properties from that row, then returns it's self?

Or should there perhaps be a clone method to create a separate instance and return that.

Thanks

Rob
#6

[eluser]Colin Williams[/eluser]
And what would cloning it achieve? It's already a new object created by the database driver. If it is conveniently already structured in the way you've intended, why do more? I'm not sure what you are looking to do in the end.

Again, the model need not be the representation of the object. I tend to treat database-based objects as methodless, and I use the model to provide the functionality that these method would typically provide themselves.




Theme © iAndrew 2016 - Forum software by © MyBB