Welcome Guest, Not a member yet? Register   Sign In
Having a model extend another model
#1

[eluser]hcker2000[/eluser]
I'm trying to figure out a good way to help keep things DRY. For my example lets say I have some basic methods for a model as follows.

Code:
get_by_id
get_by_email
get_by_token

Now lets say I have a method that is specific to the public facing side of the site.
Code:
get_current_item

Now lets say I have some methods specific to the private side of the website (or admin).

Code:
get_all_items
delete_item
update_item

What seems to be ideal is to put those generic methods that could be used by the public or private side of a website into a model called generic. then in the front end model it could extend the generic model.

Is there any good way to do this type of setup with codeigniter? or is it better to just put all those methods in one model? It seams like models could get a bit monolithic if you just put all related methods in the one model vs extending.

#2

[eluser]InsiteFX[/eluser]
MY_Model
Code:
// saveas - ./application/core/MY_Model.php
class Base_model extends CI_Model {

    public function __construct()
    {
        parent::__construct();
    }

    // your code for model 1
}

// saveas - ./application/models/name_model.php
class Model_2 extends Base_model {

    public function __construct()
    {
        parent::__construct();
    }

    // your code for model 2
}

Something like that...
#3

[eluser]hcker2000[/eluser]
Close though it just clicked in my head that I'm using this DRY methodology and it will extend fine to the models.
#4

[eluser]InsiteFX[/eluser]
You may still need the config autoload code not sure if they have fixed it yet!
#5

[eluser]hcker2000[/eluser]
I have read a bit about the config autoloading but did not know any thing was wrong with it.
#6

[eluser]InsiteFX[/eluser]
It would not load the ./application/core/files.php

Thats why Phil created the autoload for the config file!
#7

[eluser]hcker2000[/eluser]
It seams like it would be nice to have it route base on prefix of the extended class. so if it is

Code:
class account extends ADMIN_account
{

}

It would know to look for that class file at core/admin/account.php

Adds some over head but would help keep things organized.
#8

[eluser]johnnortredamme[/eluser]
Never had any problem with the config autoload code.




Theme © iAndrew 2016 - Forum software by © MyBB