![]() |
An example of easy model usage - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: An example of easy model usage (/showthread.php?tid=19178) |
An example of easy model usage - El Forum - 05-30-2009 [eluser]drewbee[/eluser] Hey all, As the continuing process to make things even more easier and allow flexibility, but conform to some kind of standard I have started doing the following in regards to models. Basically, every model will have some kind of get,update,insert and delete. Now, I know a lot of people like to be very specific with their models, but I do not like that. I like to have more control in the controller over what is being used in the model. As another point of focus and tracking, almost every table I like to have a created and updated date, a who (account id of editor), and a deleted flag. I very rarely have a table without these, so even if it is just a reference table, I include these as well 'just because'. So, first and foremost, let's extend CI's model and set our constant get, update, insert, and delete methods. Code: <?php So now, when creating a new model we simply have to specify the table name Code: class Accounts extends CIEXT_Model Usage, of course Code: $this->load->model('accounts'); Obviously this setup works strictly for the base tables, and will have to be more robust when dealing with joinings, but it gets rid of a lot of repetition for the same functionality. Thoughts on this? An example of easy model usage - El Forum - 05-30-2009 [eluser]Dam1an[/eluser] I used to do my models in a very similar way, but it then got too complex when I started to add joins etc ![]() I also had it so that I didn't even need to create a wrapper model, but could just create the base model passing the table name to the constructor and assigning it to the relevant variable name An example of easy model usage - El Forum - 05-30-2009 [eluser]drewbee[/eluser] Yup. This was intended strictly for the base table queries. It at least keeps 4 functions out of the main model. On a side note, did you end up creating a separate model for referential tables? I still havn't decided and found the exact way I want to do this yet. It either goes into the the parent model, or it's own. I could see the parent being ok, as long as that table didn't exist to multiple tables. .... Organization sucks. ![]() An example of easy model usage - El Forum - 05-30-2009 [eluser]Dam1an[/eluser] The way I do it (well, in most cases) is I have object tables and relational tables Object tables is a 'thing' and has an auto ID A relational table is... well, self explanatory really I only have models for the object tables, and they then have methods which deal with the relational tables So if I have an accounts, users and account_users table, I'd have Account_model and User_model and the relationship would be Account_model (method would be get_users) Although every now and again it just seem to logical to have the relationship in the other table (or maybe in both :-S) |