Welcome Guest, Not a member yet? Register   Sign In
Where to put custom classes?

If I have a class, say "Product", handling data/methods for a product, where would be the best place to put it? The 'libraries' folder seems like the best fit, but on the other hand object classes aren't really libraries. Is there somewhere else?

Also, is there a way to automatically put the data returned from a MySQL query into a class object? Like the FETCH_CLASS method in PDO.

There are several ORM libraries around that do exactiy that. Search for Datamapper DMZ or Doctrine.

If it's a Model, put it in /system/application/models...

If it's a library, put it in /system/application/libraries...

How can you tell the difference?

I do it like this: in my applications, Models are representative of my database and are a way for me to interact with my app's data. Libraries are classes that perform certain functions (like my Google Map API library, or my SugarCRM library), but don't represent the data in my DB.

What if you have some code that generates data but not from a database just self contained data like a registration number. Should that be a model or a library?

[eluser]Mr. Pickle[/eluser]
I normally use a model for data transaction (get / store data) whether it is from / to a database, file e.g.

Just for creating a number I would use a library I guess....
But it is very good possible to have dependencies of a model (e.g. if data from db is needed)

What do you do with the registration number? Does it become part of another object such as a User object? If so, then you can create a function within your User model to generate the registration number. If that function can be used in other places (i.e. generic), you can create it as a library or helper function instead.

Thanks for the help. I think I am going to make it a library because I pulled the code from another place and it doesn't write to a database. Should I need to move the code again I think it makes sense to be in a library and not in a model. Thanks for the help!

I'm not sure that the current answers really solve the problem (although the Datamapper DMZ looks interesting).

Let's say I have a bunch of products. When I load a product page I would use the Product_model to load the item from the database, into an anonymous stdClass. This is OK for most situations, but then if I want to operate on the data in some way I'm adding more stuff to the model (which doesn't really seem fit for that purpose). Plus you can't really create an array of Product_models.

A library doesn't seem to fit either as they just look like function collections again. It would be better to have a self-contained Product class with its own members and methods. Should I just include separate PHP files as/when I need them?

That is exactly what an ORM library does, and how I use Datamapper DMZ.

Theme © iAndrew 2016 - Forum software by © MyBB