Welcome Guest, Not a member yet? Register   Sign In
model for two tables
#1

[eluser]jared4444[/eluser]
Hi,
I am new to CI and am not sure what to do.

I was building models based on tables and it worked well.
I have these tables
category
item

categories contain many items.

When I want to get all items from a category, how should I do that? Should I make another model category_item? Is there a better way?

I am confused as to how to proceed when I am dealing with more than one table at a time.
#2

[eluser]Krystian[/eluser]
try to write in model method that returning data do show on the screen in the view.

sql
Code:
SELECT itemName FROM categories INNER JOIN items ON &lt;link conditions&gt; WHERE categoryId = <here parameter>

you can bind it or something.....
#3

[eluser]davidbehler[/eluser]
You can use as many tables per model as you like. True, many out there try to use only 1 table per model, but seriously: Why?

Most of the time 1 table depends on the other (like in your case) and then you have to mix them anyways. I usually have 1 model per controller that deals with whatever tables the controller needs. If there happens to be the same function in multiple models, I create a common model that contains the functions that otherwise would be duplicate.
#4

[eluser]jared4444[/eluser]
Thanks, but I don't think I was too clear in my question.

My question is:
1. If I have a model for each table item = item_model, category = category_model, what do I do when I will be accessing more than one table? Do I put category info in item_model? Do I put item info in category_model? Do I make another category_item_model?
#5

[eluser]Krystian[/eluser]
jared you really like to make your life difficult Smile
I agree and using waldmeister`s approach, it`s easy to use, flexible, intuitive, etc.
...
#6

[eluser]jared4444[/eluser]
[quote author="waldmeister" date="1262576908"]You can use as many tables per model as you like. True, many out there try to use only 1 table per model, but seriously: Why?

Most of the time 1 table depends on the other (like in your case) and then you have to mix them anyways. I usually have 1 model per controller that deals with whatever tables the controller needs. If there happens to be the same function in multiple models, I create a common model that contains the functions that otherwise would be duplicate.[/quote]

Thanks, this is the approach I will take. I think we posted our comments at the same time, which is why I missed your response. Thanks!
#7

[eluser]davidbehler[/eluser]
Glad I could help Smile
#8

[eluser]2think[/eluser]
Jared4444,

Now saw that waldmeister solved your case, all's well that ends well
#9

[eluser]jared4444[/eluser]
[quote author="waldmeister" date="1262576908"]You can use as many tables per model as you like. True, many out there try to use only 1 table per model, but seriously: Why?

Most of the time 1 table depends on the other (like in your case) and then you have to mix them anyways. I usually have 1 model per controller that deals with whatever tables the controller needs. If there happens to be the same function in multiple models, I create a common model that contains the functions that otherwise would be duplicate.[/quote]

Hi Again, I have a quick follow up.

I have the function below and get a row from my db. I was wondering if you would do two database calls, one to get the row, then one to get the associated rows. If not, how would you combine them into a single call and fill everything you need to fill?

Here is my function. setfromdbrow assigns db columns to variables that I declare in the model.

Also, would you declare an array in the top of the model with nothing in it, just as a place holder?

Thanks

class Item_Model extends MY_model {
var $_tableName = 'Item';
var $name = '';
var $description = '';


//this is from MY_model
//hydrates this instance with info from db
function get($id)
{
$this->db->select('*');
$this->db->where('id =', $id);
$query = $this->db->get($this->_tableName);
if ($query->num_rows() > 0)
{
$row = $query->row();
$this->setfromdbrow($row);
}
else
return false;
}
#10

[eluser]davidbehler[/eluser]
There is nothing wrong with using a join (if it's a 1:1 relation) or loading the associated row(s) (if it's a many:many or 1:many relation) in the same function after getting the original row. Do whatever feels easiest to you.




Theme © iAndrew 2016 - Forum software by © MyBB