Welcome Guest, Not a member yet? Register   Sign In
Intermediate subclassing of DataMapper class to add common fields/behaviors?
#1

[eluser]mscott809[/eluser]
My database model has the same core fields on every data table, like created_at and updated_at, which I see are supported, but also other fields like "inactive" and "last_mod_by", as well as a common to-many relationship (notes that can be attached to any other object). I would like to implement an intermediate class to handle the maintenance of these fields and related notes, but this intermediate class is not representing any one table by itself, just common goo for most of my tables. For instance, I'd like to do something like this:
Code:
class MyBaseModel extends DataMapper { ... }

class User extends MyBaseModel { ... }

But MyCommonModel doesn't represent a table in the database, just common columns and behaviors I want all objects to have.

Can I do this? If it just means having to manually set $model and $table on every sub class and dynamically update the $has_many array to insert the "notes" relationship, that would be well worth the ability to consolidate this common code. But I fear it's not that easy.

Thanks.
#2

[eluser]WanWizard[/eluser]
Yes, why not?

You only need to load MyBaseModel manually, as CI doesn't have any autoloading capabilities, and Datamapper will not load this for you, as it is not a model.
#3

[eluser]mscott809[/eluser]
I figured it might be a problem because DM is clearly looking at the subclass for some things (like table name). Are the things I listed in my original post all that I'd need to manually set to allow for this kind of class hierarchy, or might there be other things, too?

Thanks!
#4

[eluser]WanWizard[/eluser]
You are correct in that some things are derived from the model class name with you didn't configure anything, but that shouldn't be a problem.

In your example, the model name is 'User', which will generate a tablename 'users'. Like it should. It doesn't see your base class.

There are some instanceOf checks, but that works for grandparents too (for User, instanceOf Datamapper will return true).
#5

[eluser]mscott809[/eluser]
Perfect! I will head down this route and ask back here if anything breaks (assuming I can't figure it out on my own, of course).

Thanks!
#6

[eluser]WanWizard[/eluser]
Let me know how things go, it might be worthwhile mentioning this approach in the docs.




Theme © iAndrew 2016 - Forum software by © MyBB