![]() |
model for two tables - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: model for two tables (/showthread.php?tid=26003) |
model for two tables - El Forum - 01-03-2010 [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. model for two tables - El Forum - 01-03-2010 [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 <link conditions> WHERE categoryId = <here parameter> you can bind it or something..... model for two tables - El Forum - 01-03-2010 [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. model for two tables - El Forum - 01-03-2010 [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? model for two tables - El Forum - 01-03-2010 [eluser]Krystian[/eluser] jared you really like to make your life difficult ![]() I agree and using waldmeister`s approach, it`s easy to use, flexible, intuitive, etc. ... model for two tables - El Forum - 01-03-2010 [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! model for two tables - El Forum - 01-03-2010 [eluser]davidbehler[/eluser] Glad I could help ![]() model for two tables - El Forum - 01-03-2010 [eluser]2think[/eluser] Jared4444, Now saw that waldmeister solved your case, all's well that ends well model for two tables - El Forum - 01-11-2010 [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; } model for two tables - El Forum - 01-12-2010 [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. |