Welcome Guest, Not a member yet? Register   Sign In
Extending the default model
#1

[eluser]dieter[/eluser]
Is it possible to extend CI's default Model? I've tried creating an extending MY_Model in app\libraries\ but I get an error saying that the class can't be found. Somewhere else on the forum, I found a hack to solve this problem by adding a require_once in every model, but shouldn't this be handled by CI itself (that's why we use a framework after all). Or am I overseeing something?

Thanks,
Dieter
#2

[eluser]Michael Wales[/eluser]
Yes - you can extend the default model (I do it on all my application with my ORM library, which I'll be releasing soon).

libraries/MY_Model.php:
Code:
class Orm extends Model {
}
#3

[eluser]koorb[/eluser]
I think the problem is dieter is seeing is caused by this http://codeigniter.com/bug_tracker/bug/3102/. Model is not loaded like other core libraries and cannot be extended using the MY_x extends CI_x method. Im not sure why it is done this way, one of the mysteries of CI. But it does cause problems I am currently trying to add some basic common methods to all my models and therefore would like to extend Model to implement them. I will let you know what I end up doing.

@dieter: I'm presuming the class that can't be found is 'Model'. The reason you'll get that is because of the way Model is loaded like I mentioned above, but if you require BASEPATH . '/libraries/Model.php' in your MY_Model.php that should work.

Edit: Incidentally I don't seem to be able to use __get and __set methods because CI instance properties are assigned to Model by reference and PHP wont allow referenced attributes on overloadable classes
#4

[eluser]treehousetim[/eluser]
if you create library/MY_Model.php...

Code:
class MY_Model
{

//method here
}

Then in your regular model files, you need to extend from MY_Model...

Code:
class myTable extends MY_Model
{
//blah
}

This works - I'm using it all the time. If you continue to get errors, please send more info.
#5

[eluser]wiredesignz[/eluser]
Don't bother extending CI's model, just copy it to application/libraries and add your own functionality.
#6

[eluser]taewoo[/eluser]
I have a similar issue.
I am creating an abstract class that extends the CI model. My sub-models extend this abstract model (inheriting the methods) with different table name via the constructor

Code:
super('tableName')

PHP is complaining that this generic, abstract model cannot be found (even though it's in the same directory) when a controller tries to load the extended sub-model. Is this a path issue or do I need to go wiredesignz method?
#7

[eluser]treehousetim[/eluser]
At the risk of sounding like a noob (I'm not), and even though I have searched the CI code base and Googled for the function "super" you're calling - I'm clueless as to what it does.

I think we might need more info about what you're trying to do.


[quote author="taewoo" date="1215657378"]I have a similar issue.
I am creating an abstract class that extends the CI model. My sub-models extend this abstract model (inheriting the methods) with different table name via the constructor

Code:
super('tableName')

PHP is complaining that this generic, abstract model cannot be found (even though it's in the same directory) when a controller tries to load the extended sub-model. Is this a path issue or do I need to go wiredesignz method?[/quote]
#8

[eluser]taewoo[/eluser]
Oh, oops. Maybe I meant "parent::Constructor()". Smile

This is what i'm trying to do
Code:
<?php
abstract class Generic_model extends Model {

    private $tableName;
    
    function Generic_model($tableName)
    {
        parent::Model();
        $this->tableName = $tableName;
    }
    
    function edit($id, $data)
    {
        if(empty($id))
        {
            return FALSE;
        }
        $this->db->where('id', $id);
        $this->db->update($this->tableName, $data);
    }
    
    function delete($id)
    {
        $this->db->where('id', $id);
        $this->db->delete($this->tableName);
    }
    
    
}
?>

And the sub-class

Code:
<?php
class Club_event_model extends Generic_model {

    function Club_event_model()
    {
        parent::Generic_model('club_event');
    }
}
?>
#9

[eluser]roguewave[/eluser]
Did you figure out a way to pass the table name for a model?
#10

[eluser]treehousetim[/eluser]
[quote author="roguewave" date="1218760150"]Did you figure out a way to pass the table name for a model?[/quote]

I simply do this:

Code:
$this->tableName = 'mytable'

in my constructor

Full example:

Code:
class mytable extends MY_Model
{
    function mytable()
    {
        parent::MY_Model();
        $this->tableName = 'mytable';
    }
}

class MY_Model extends Model
{
    function MY_Model()
    {
        parent::Model();
    }

    function edit()
    {
        $fieldArray = array(); // you need to create this per the manual

        $this->db->where( 'id', $this->id );
        $this->db->update( $this->tableName, $fieldArray );
    }
}

In practice my code is much more complex, but this is the basics.




Theme © iAndrew 2016 - Forum software by © MyBB