Welcome Guest, Not a member yet? Register   Sign In
Creating Models
#1

[eluser]Rey Philip Regis[/eluser]
Hi guys,

Here I am again asking questions haha....Well actually What Im going to ask is not about syntax for code igniter. Actually I can ask this question other php framework communities like Zend, CakePHP and others, but since Im here in the CodeIgniter community and Im now learning how to code using CodeIgniter and I stayed here cause Im very comfortable using it, so I planned to asked the expert coders here.

Im going to ask about the "M" on the M-V-C which is the model. Is it advisable to have a model using joins?

Sample:

category_table
id | category_name
1 | cellphone
2 | processors
3 | speakers
4 | monitors

products_table
id | category_id | product_name
1 | 1 | nokia N96
2 | 1 | nokia N81
3 | 2 | intel quadcore 2.4
4 | 2 | amd phenom
5 | 3 | altec lansing VS4221
6 | 3 | creative inspire 2.1
7 | 4 | LG Flatron L17753S
8 | 4 | Samsung T220

Now....What I want to do is that I want to have an output like this
category_name | product_name
cellphone | nokia N96
cellphone | nokia N81
processors | intel quadcore 2.4
processors | amd phenom
speakers | altec lansing VS4221
speakers | creative inspire 2.1
monitors | LG Flatron L17753S
monitors | Samsung T220

So, if Im going to have this output, I will do a join syntax in SQL.
My question now is this, is it advisable to have a model using join?
And if its okey (meaning advisable) what is the name of the model product_category_model?
I need an advise from the experts. Cause Ive seen in other frameworks that models only maps
one table, so for example categiry_table so you will have a category_table class "Model".
So how about my scenario?

Thanks in advance.....

Good day.
#2

[eluser]m4rw3r[/eluser]
There are nothing to prevent you from using two or more tables in a single model in CI.
In the situations where the tables are not used without each other you should use a single model for both tables, because you wouldn't want to always merge the result in the controller.
It is also possible to have models "overlap" (use the same tables, but in different ways), but the gain of that depends on the db schema and the usage of the data.

About using a join, that is advisable because otherwise you would be using n + 1 queries (n = number of categories).




Theme © iAndrew 2016 - Forum software by © MyBB