• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic MY_Model question. Need some help

#1
[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)
{
  $this->db->update('posts', $values, array('post_id' => $post_id));
  return TRUE;
}

public function edit_user($values, $user_id)
{
  $this->db->update('users', $values, array('user_id' => $user_id));
  return TRUE;
}
I want to combine those like:
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?

#2
[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)

#3
[eluser]Learn CodeIgniter[/eluser]
Code:
public function edit_record($table, $values, $str_id, $id)
{
    $this->db->update($table, $values, array($str_id => $id));
    return TRUE;
}

#4
[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.

#5
[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

#6
[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';
Sometimes the primery key is `order_id`, sometimes is `vehicle_id` etc.

#7
[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.

#8
[eluser]victorche[/eluser]
One last question... How can I optimise this:
Code:
public function all_records($table, $where = array(), $field = FALSE, $order = 'ASC')
{
  if (is_array($where) && count($where))
  {
   foreach ($where as $key => $value)
   {
    $this->db->where($key, $value);
   }
  }

  if ($field)
  {
   $this->db->order_by($field, $order);
  }

  $query = $this->db->from($table)
         ->get();

  return $query->result_array();
}
The problem is that I don't always have a where clause. And if I have an order_by clause, then I have to call this function like:
Code:
$posts = $this->model_something->all_records('posts', '', 'author');
Which is ugly and I want to optimise it Smile

#9
[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) )
{
    // This is easier for your and does the same as your code above did since the
    // db->where method supports associative arrays
    $this->db->where();
}
// If $where is a string we just assume that it is the field we want to order
// by. Since you define your API, you can define it anyway you want ;)
elseif ( is_string($where) )
{
    // We just shift all arguments left by one (i.e. $where is now $field,
    // $order is now $field)
    $field = $where;
    // Just to ensure we have an "ASC" or "DESC" ordering and not something invalid
    $order = strtoupper($field) == ("ASC" OR "DESC") ? : "ASC";
}

// Now just process the code as you did above since the arguments are assigned to
// the right variables
if ( $field && $order )
{
    // Again just to ensure we have an "ASC" or "DESC" ordering and not something
    // invalid
    $order = strtoupper($order) == ("ASC" OR "DESC") ? : "ASC";
    
    $this->db->order_by($field, $order);
}

// The rest of your code goes here (select from and get)


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.