Welcome Guest, Not a member yet? Register   Sign In
Extending Datamapper
#1

[eluser]GregX999[/eluser]
Hey guys,

I'm using DataMapper and I have a few methods that I find myself placing in every, or almost every model. So I wanted to create a class (ie: DataMapperExtra) that extends DataMapper, and contains those methods. But if I do that, I get an error that DataMapper can't find the database table "datamapperextras".

As soon as I saw that error, I understood what was going on. But is there a way to "extend" the DataMapper class without using "extends DataMapper"? I really don't want to have to include the same code over and over in my models, nor do I want to edit the main DataMapper.php file.

Greg
#2

[eluser]WanWizard[/eluser]
Your DataMapperExtra class can't have a constructor. The current Datamapper constructor checks if the class loaded it 'Datamapper' (on which it does some setup) or not, in which case it is assumed to be a model.

To work around it, you can try
Code:
<?php
include "datamapper.php";

class DatamapperExtra extends Datamapper
{
}

and store this in application/libraries/datamapperextra.php.

Additionally, you need to modify datamapper.php:
Code:
//change
        $is_dmz = $this_class == 'datamapper';
// to
        $is_dmz = $this_class == 'datamapperextra';

After this, your models should be able to extend Datamapperextra instead of Datamapper.

Currently I don't see another option.
#3

[eluser]GregX999[/eluser]
I tried that and now I get the error:

Fatal error: Call to undefined function singular() in /.../application/libraries/Datamapper.php on line 472

Any ideas?
#4

[eluser]WanWizard[/eluser]
Hmm... This is not that simple.

Did you do a $this->load->library('datamapperextra')? That should trigger the __construct() which loads the required helpers, if you have made the change mentioned above.

But it gets worse. The Datamapper class is used both statically and as parent for your models. If you extend it, and use your extended class for your models, static calls to Datamapper will no longer work.

I'm afraid this needs structural changes, which means next major release.

Could you create a ticket on http://bitbucket.org/wanwizard/datamapper/issues so this isn't forgotten?
#5

[eluser]GregX999[/eluser]
Ok, I added the ticket.

I guess in the meantime I'll either add the methods to datamapper.php, or else create a php file of methods that all the models can "include". (Any other ideas for a temp fix?)
#6

[eluser]WanWizard[/eluser]
I think the 'smartest' thing to to at the moment is to create an extension, put your methods in there. Autoload the extension through the config, and all your models can access the methods.

No need to change both Datamapper and your models, also no need to extend the class.




Theme © iAndrew 2016 - Forum software by © MyBB