Welcome Guest, Not a member yet? Register   Sign In
How many classes are allowed for each model php file?
#1

[eluser][email protected][/eluser]
Hello, ci pros Smile

Is there any suggested number for each? Or its recommanded to put all extended classes inside one model php file?
If it does allow multi-extended-classes in one model file, how can I call it in this case? Can I have a example?

Thank You!
#2

[eluser]pzntec[/eluser]
I think you have the whole concept of MVC (model-view-controller) is wrong. I suggest you understand the structure and which file should contain what.

My personal recommendation is to use one model for a corresponding controller. If you don't, things can get very confusing if your site was to get extremely big!

Lastly, consider using an HMCV structure to make thing much easier in the long run.
#3

[eluser]Pert[/eluser]
[quote author="[email protected]" date="1370287134"]
Is there any suggested number for each? Or its recommanded to put all extended classes inside one model php file?
If it does allow multi-extended-classes in one model file, how can I call it in this case? Can I have a example?
[/quote]

You should only have single model in single model .php file.

Model files are automatically added based on file and class name, so if you are loading one file with multiple models, only once model class is attached to CodeIgniter properly. If you are trying to load any other models, it will throw error message, because model .php file was not found.

Model should be treated as single logical "object" in your application, not one model for every controller.

Lets say you have a online shop. A model would be product, and you can use that model all over your site in different controllers, whenever you have to deal with products. Or model could be a category, and that category model could have helper methods that return product models for active category.
#4

[eluser]boltsabre[/eluser]
Each "model" is a class in its own right.

As to how you structure them, that really depends on your application. Some people go for a model per DB table basis (personally I'm not a fan of that).

You can also break them into "use cases". As per Pert, and his excellent example of a "product" model. In a smaller application it may be fine to have one "super" product model, but in a larger application it may make more sense to break it into small bits of code on a "use case" basis, perhaps a "checkout_product_model" which could have such functions as "add_to_cart", "remove_from_cart", "check_product_exists", "check_product_is_in_stock", "calcuate_product_quantity", etc (although those last three may be better in a parent product model which the other product models would extend upon. Then you may also have a "purchase_product_model" which would have functions for updating inventory counts, tax, receipts, etc.

You'll get more of an idea as you play around with the MVC concept, start with building a few smaller projects. Then try a medium sized one.
#5

[eluser][email protected][/eluser]
Thanks for all of yours help! Very helpful!

I have another question comming up please stay tuned....lol
#6

[eluser]boltsabre[/eluser]
lol, look forward to it!




Theme © iAndrew 2016 - Forum software by © MyBB