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

[eluser]jmadsen[/eluser]
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...

http://www.codebyjeff.com/blog/2012/01/u...s-my_model
#2

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

[eluser]CroNiX[/eluser]
[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.
#4

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

[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
Code:
//$config_validation[your_table][field]
$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:
Code:
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!
#6

[eluser]theshiftexchange[/eluser]
Is there any way to do simple joins with this? Or do I need to re-write those functions?
#7

[eluser]theshiftexchange[/eluser]
found the answer myself - incase anyone is looking for it- use a "before_get" callback, and run a join in there.
#8

[eluser]theshiftexchange[/eluser]
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.

Code:
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)
#9

[eluser]theshiftexchange[/eluser]
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.

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


         function __construct()
{
  parent::__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
    unset($post[$post_id]);
   }
  }

  // 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
  unset($post[$this->primary_key]);

  // 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