Welcome Guest, Not a member yet? Register   Sign In
where to put database helpers (driver, active record, or helper)
#1

[eluser]a&w[/eluser]
I perused the mysqli_driver and DB_active_rec classes (as well as the user guide and forum of course).

I have repetitive needs to prepare a query with orderby, sortby and other where conditions. I wasn't quite sure where to stick these "helpers". Should I just make my own helper class, or extend either the driver or active record class?

My first impressions:

HELPER
pros: Don't "mess" with any core classes whatsoever
cons: Will need to load the helper class

ACTIVE RECORD
pros: Already available to model without loading
cons: I'm not sure the strings I'd generate would apply to other db drivers (but not likely use a different driver anyway)

MYSQLi DRIVER
pros: Already available to model without loading
cons: "Messing" with the core class, but if only adding methods shouldn't circumvent future CI releases?

So my inclination (at the moment) might be to extend the mysqli_driver.
#2

[eluser]Developer13[/eluser]
I'm not quite sure why you are not just considering building that functionality into your models... CI's database class more than supports what you are trying to do...
#3

[eluser]a&w[/eluser]
I didn't see where there's anything for sort or order by. Also, I'll have multiple models which could use a helper utility to build the query. Since I'd being using similar code multiple times and since the existing class(es) don't have all the functions I want I thought to extend or provide a helper in some way.
#4

[eluser]Sam Dark[/eluser]
You can use base model (MY_Model).
#5

[eluser]a&w[/eluser]
Ok, I used MY_Model. I'll post what I did in case someone else stumbles onto this thread:

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Model extends Model {


    function __construct()
    {
        parent::Model();    
    } //end __construct

    
    function someExtendedMethod()
    {
        echo 'dude';
    } //end someExtendedMethod
    
} // END MY_Model Class

/* End of file MY_Model.php */
/* Location: ./application/libraries/MY_Model.php */

and
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Some_model extends MY_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::Model();

        // Call the extended method from MY_MOdel
        $this->someExtendedMethod();
    } //end __construct

} // END Some_model Class

/* End of file some_model.php */
/* Location: ./application/models/some_model.php */

Two semi-related links:
extend Model class
Extending the core: MY_Model




Theme © iAndrew 2016 - Forum software by © MyBB