Welcome Guest, Not a member yet? Register   Sign In
A "User's Guide" to Jamie Rumbelow's MY_Model library

I wasn't sure if this is appropriate, at it links to my own site's blog, but I put together a bit of a "user's guide" for people new to CodeIgniter to encourage them to start using a MY_Model class.

Joost van Veen just came out with a great codeigniter.tv video series on creating your own MY_Model, and Jamie's code has been around for a while (and is the basis of the one on PyroCMS), so I didn't want to repeat their efforts. A lot of people recommend Jamie's library to new users, but I think it is a little confusing to them.

So my post was simply to walk through all of the functions on it and give an example of each one's use, with the output it gives. Hopefully it will give people a jump-start on using it.

So, for better or worse...


[eluser]Mauricio de Abreu Antunes[/eluser]
Is there missing join, like or something else in MY_Model?
Lemme know if i can add more methods in MY_Model class.

[quote author="Mauricio de Abreu Antunes" date="1330628036"]Is there missing join, like or something else in MY_Model?
Lemme know if i can add more methods in MY_Model class. [/quote]
Anything you add to a MY_Controller or a MY_Model will be available to all controllers and models, respectively. So, yes, you can add whatever common methods that you need.

[eluser]Mauricio de Abreu Antunes[/eluser]
I know.
Anyway thanks! I'll add some methods in this model and post here.

[eluser]Mauricio de Abreu Antunes[/eluser]
IMHO, validation must to be in controllers.
MY_Model has a validation config.
How can i return error messages in Model?

I was thinking:
1 - Create a config like
$config_validation['news']['title'] =
array('title', 'Title', 'required|xss|min_length[25]|max_length[100]');
$config_validation['news']['tag']    =
array('tag', 'News Tag', 'xss|min_length[1]|max_length[20]');

2 - Create a method in MY_Controller:
public function checkValidation ($table, $arr_fields) {
//Loop array with table ($table) index checking fields and setting validation
foreach ($arr_fields as $f) {
  $this->form_validation->set_rules($config_validation[$table][$f][0], $config_validation[$table][$f][1], $config_validation[$table][$f][2]);
Did you get my point?

PS.: i did not check my code, just for show my ideas!

Is there any way to do simple joins with this? Or do I need to re-write those functions?

found the answer myself - incase anyone is looking for it- use a "before_get" callback, and run a join in there.

I wanted a better solution to mySQL date conversions. In my application, dates are "DD-MM-YYYY" due to the local area. But in mySQL they are "YYYY-MM-DD". So I used callbacks to always flip the date prior to retrial and storage of dates.

class Fake_model extends MY_Model

public $before_update = array ('date_convert_to_mysql');
public $before_create = array ('date_convert_to_mysql');
public $after_get = array ('date_convert_to_php');

// Make all dates in DD-MM-YYYY format for our app (from YYYY-MM-DD)
protected function date_convert_to_php($result)
  $result->purchase_date = date("d-m-Y", strtotime($result->purchase_date));
  return $result;

// Make all dates in YYYY-MM-DD format for mySQL (from DD-MM-YYYY)
protected function date_convert_to_php($post)
  $post['purchase_date'] = date("Y-m-D", strtotime($post['purchase_date']));
  return $post;

Works perfectly for me for single inserts/updates and single/multiple gets. No more date format issues!!!

(Havent tested for multiple inserts/updates)

Another idea I'm using - White_list your insert data (after form_validation) so you can just pass the $this->input->post() result and sleep easy at night.

class Fake_model extends MY_Model
public $before_create = array ('white_list_add');

         function __construct()

// Make sure only the correct data is loaded
protected function white_list_add($post)
  // Get all the field names once, so we dont smash the database
  $fields = $this->db->list_fields($this->_table);

  // Go through all the Post data
  foreach ($post as $post_id => $value)
   // Now check if the post is equilivant to a column in the table
   if ( ! in_array($post_id, $fields))
    // If not, then unset it

  // Also make sure the ID field is always removed on creation - the database should pick it and prevent a hacker from overriding someone elses record

  // And now manually set the user_ID - so even if someone tried to hack a new ID, we overwrite it regardless (for additional safety, if your validation did not check this - obviously alter this depending on your specific needs)
  $user= $this->ion_auth->user()->row();
  $post['users_id'] = $user->id;

  // Now return the cleaned data
  return $post;        

Theme © iAndrew 2016 - Forum software by © MyBB