Welcome Guest, Not a member yet? Register   Sign In
Libraries or Models, favouritism?
#10

[eluser]xwero[/eluser]
Why isn't a model as portable as a library? At some point in you code you need to access the actual field of the actual table of the actual database. If you put these actual values in the library you have to change the library code. If you put it in a model you have to change them in the model. If you do this
Code:
function check($nr)
{
    $query = $this->ci->db->get('phone_ranges');
    $valid = array();
    foreach($query->result() as $row)
    {
        $valid[] = $row['range'];
    }
    if(in_array(substr($nr, 0, 3), $valid)) {
     if(reg_exp('#4/\g\n.gdd[3]d')) {
        return TRUE;
     }
  }
  return FALSE;
}
You can only use that query in that library. If you put it in a model you can call the query in any library you want.

I will go further with the example you provided. You want to add a custom rule to the validation library. Therefore you can extend the validation library with application/libraries/MY_Validation.php
Code:
class MY_Validation extends CI_validation
{
    function MY_Validation()
    {
       parent::CI_validation();
    }

    function valid_phone($str)
    {
        $valid = $this->Validationmodel->getRanges('phone_ranges');
        if(in_array(substr($nr, 0, 3), $valid)) {
            if(reg_exp('#4/\g\n.gdd[3]d')) {
                 return TRUE;
            }
        }
        return FALSE;
    }
}
Now it you can use this function just like a regular rule (don't forget to add the rule to the language file). If you run multiple apps of one CI all your apps have instant access to that custom rule. If the different apps use different database structures you can define a constant in your bootstrap file to identify the app and in your model you can do
Code:
function getRanges()
{
    switch(APPNR)
    {
        case 1:
          $tablename = 'table1';
        case 2:
          $tablename = 'table2';
    }
    $query = $this->ci->db->get($tablename);
    $valid = array();
    foreach($query->result() as $row)
    {
        $valid[] = $row['range'];
    }
    return $valid;
}

If you put the database code in the library you would have to change the code there making the amount of code bigger. I prefer short files in spite of the fact i do a search for the method i want to change.

Models are a great way to separate data actions from user actions and to keep your code easy to maintain.


Messages In This Thread
Libraries or Models, favouritism? - by El Forum - 02-25-2008, 08:28 AM
Libraries or Models, favouritism? - by El Forum - 02-25-2008, 10:41 AM
Libraries or Models, favouritism? - by El Forum - 02-25-2008, 10:46 AM
Libraries or Models, favouritism? - by El Forum - 02-25-2008, 02:59 PM
Libraries or Models, favouritism? - by El Forum - 02-25-2008, 04:54 PM
Libraries or Models, favouritism? - by El Forum - 02-26-2008, 01:33 AM
Libraries or Models, favouritism? - by El Forum - 02-26-2008, 01:43 AM
Libraries or Models, favouritism? - by El Forum - 02-26-2008, 02:13 AM
Libraries or Models, favouritism? - by El Forum - 02-26-2008, 02:24 AM
Libraries or Models, favouritism? - by El Forum - 02-26-2008, 03:05 AM
Libraries or Models, favouritism? - by El Forum - 02-26-2008, 03:20 AM
Libraries or Models, favouritism? - by El Forum - 02-26-2008, 03:34 AM
Libraries or Models, favouritism? - by El Forum - 02-26-2008, 03:42 AM
Libraries or Models, favouritism? - by El Forum - 02-26-2008, 03:50 AM
Libraries or Models, favouritism? - by El Forum - 02-26-2008, 04:26 AM
Libraries or Models, favouritism? - by El Forum - 02-26-2008, 08:08 AM
Libraries or Models, favouritism? - by El Forum - 02-26-2008, 08:40 AM
Libraries or Models, favouritism? - by El Forum - 02-26-2008, 10:23 AM
Libraries or Models, favouritism? - by El Forum - 02-26-2008, 10:33 AM
Libraries or Models, favouritism? - by El Forum - 02-26-2008, 11:34 AM
Libraries or Models, favouritism? - by El Forum - 02-26-2008, 01:01 PM
Libraries or Models, favouritism? - by El Forum - 02-26-2008, 01:41 PM
Libraries or Models, favouritism? - by El Forum - 03-29-2008, 10:14 PM
Libraries or Models, favouritism? - by El Forum - 03-29-2008, 10:26 PM
Libraries or Models, favouritism? - by El Forum - 03-29-2008, 10:30 PM



Theme © iAndrew 2016 - Forum software by © MyBB