Basic MY_Model question. Need some help |
[eluser]victorche[/eluser]
Please, give me an advice on this one. I am creating a really simple MY_Model.php with basic CRUD functions (I know there are some really good ones, but I can not use them, because I have to change a lots of code). And because I have a lots of similar functions, I've decided to combine them. I've checked some really good pieces of code, but all those MY_Model classes suggest that the primary key is always `id`. My case is not like this. All my tables have different column names for `id`. For example... My `posts` table has `post_id` as a primary key. Table `users` has `user_id` and so on. Instead of this: Code: public function edit_post($values, $post_id) Code: public function edit_record($table, $values, $id) So, how to create a simple update function, when my column name is always different? Is there a way to get the primary key column?
[eluser]jmadsen[/eluser]
You are going about MY_Model completely incorrectly. My advice is, follow Joost's excellent tutorial http://codeigniter.tv/a-11/Extending-the...del-part-1 or just use Jamie Rumbelow's version (and look at my user's guide http://www.codebyjeff.com/blog/2012/01/u...s-my_model)
[eluser]Learn CodeIgniter[/eluser]
Code: public function edit_record($table, $values, $str_id, $id)
[eluser]victorche[/eluser]
Thanks for the answer! I've seen those tutorials already. As I said, I don't need something like this (even if it is a really good piece of code, but for a new project), because if I try to use it, I have to change a lots of code. I just want something really simple. And once again... Even in Jamie Rumbelow's class we are talking about a primary key with `id` as a name. Which is not my case. For example, how you will use this update functions, if the primary key is `user_id`? I just want to know how to make really simple "create", "delete", "update" functions... Which should be universal.
[eluser]jmadsen[/eluser]
No, you shouldn't have to change any code, which makes me think you don't fully understand them. You make a master class MY_Model, then each table gets My_table extends MY_Model and you make your changes in My_table. For example, MY_Model has $primary_key that defaults to id ...My_table sets its own $this->primary = $user_id Or, if all of your tables use <table_name>_id, then just override/change your code in one place to make it default that way instead
[eluser]victorche[/eluser]
@jmadsen, thanks for your help. Anyway I have a complete, finished project. And all those tutorials are just for... learning. I am not a database designer guru, but let's say we have a rent'a car business. I have a controller orders.php, where a new order is added. The model is orders_model.php and the table is `orders`. But I also need to get the list of all available cars (with type, model, engine). And those cars are in another table, called `cars`. So my models are complex and when I have orders_model.php, I don't use only one table is this model (creating a new order needs a dropdown with all the cars, right?). So, you want to tell me that in my orders_model.php I have to deal only with the orders table? Well, this is not my case. Making a blog tutorial is good, but not enough here. When we are speaking of tutorials, we always have "posts" case, with "posts" table and "post_id". I am using data from several tables in one model, so I can not just make Code: protected $primary_key = 'order_id';
[eluser]PhilTem[/eluser]
If you want to do it in a proper fashion, each model only knows of one table and access only this one table. For the case where you want to combine data from two tables, you would actually use a library that performs your desired tasks. You may want to have a look at my MY_Model which can be found here and the barely complete documentation can be found here. With my MY_Model each class extending this parent class can have their own definition of the table with different primary keys and all that fancy stuff. The documentation linked above isn't completed yet but you can read through the class's code itself, it should be documented well.
[eluser]victorche[/eluser]
One last question... How can I optimise this: Code: public function all_records($table, $where = array(), $field = FALSE, $order = 'ASC') Code: $posts = $this->model_something->all_records('posts', '', 'author'); ![]()
[eluser]PhilTem[/eluser]
You might want to set the function header to Code: function all_records($table, $where = array(), $field = NULL, $order = NULL) and then maybe something like Code: if ( is_array($where) && count($where) ) |
Welcome Guest, Not a member yet? Register Sign In |