How to attach an Observer to JTable

622 views
Skip to first unread message

Clubnite

unread,
Dec 11, 2013, 2:06:45 PM12/11/13
to joomla-de...@googlegroups.com
Hello,
i'm motivated to practice clean coding and prevent code duplication and core hacks. Because of this i am looking to find an explanation on how to attach an Observer to any of my own component's tables. This topic of OOP is new to me and almost clear in theory, but i somehow can't adopt out how to implement this from the tags component.

Someone else probably can serve with Joomla! framework related explanatory example?

Bakual

unread,
Dec 11, 2013, 2:32:49 PM12/11/13
to joomla-de...@googlegroups.com
You can either do a system plugin which maps the observer to the table, if you for whatever reason really need an uncoupled system. The code for this would be similar to this:
JObserverMapper::addObserverClassToClass('JTableObserverTags', 'JTableContent', array('typeAlias' => 'com_content.article'));
The downside is that you need a plugin which
  • is always processed, needed or not (you only really need it when you edit an item)
  • could be disabled by the user
  • doesn't work from CLI.

Or you have to make this call in each place where you interact with the table, which doesn't follow DRY principles.

Personally, I have no reason to decouple it from the table at all, as I want my table to always load the observers as well. Thus I put it into the construtor of my table:
TableObserverTags::createObserver($this, array('typeAlias' => 'com_content.article'));
It doesn't strictly follow the observer pattern as the table in this case is aware of at least some of its observers, but I can live with that easy. For me it's following KISS and DRY here :)
Message has been deleted

Clubnite

unread,
Dec 11, 2013, 4:36:11 PM12/11/13
to joomla-de...@googlegroups.com
Thanks for your response, Bakual.

Personally, I have no reason to decouple it from the table at all, as I want my table to always load the observers as well.

I agree to this point of view. The thing i had otherwise to put within a function override (publish method) must not be of interest to my other tables but to a specific one only. I'm aware of the publish method triggering the onChangeState event, but this stage is way to late for me since i need to catch the data before it is stored into the database. Unfortunately the publish method doesn't trigger the onBeforeStore event. So i have to choose whether to copy the JTable::publish method 1:1 into my table just for my little tweak to put into it or to leave JTable::publish as is and thus stay safe from future changes to this class through core updates. The latter is my choice.
So i'm gonna trying to implement your latter suggestions, but wonder what you mean with

It doesn't strictly follow the observer pattern as the table in this case is aware of at least some of its observers

This to me sounds like the table might miss other observers. If so, why would it? If not, how else is it meant?

Another thing stays unclear to me. You write

TableObserverTags::createObserver

Is this a typo and you meant JTable? If not, is TableObserverTags a custom table? Would you mind to make clear, which files and classes in summary must be created and how must they be tight together to introduce each other and to make them work together? 

Bakual

unread,
Dec 12, 2013, 2:34:09 AM12/12/13
to joomla-de...@googlegroups.com


It doesn't strictly follow the observer pattern as the table in this case is aware of at least some of its observers

This to me sounds like the table might miss other observers. If so, why would it? If not, how else is it meant?

No, it will work fine. It's just a theoretical statement about the observer pattern. This pattern was designed so the table and the observer wouldn't know each other, meaning there is no specific code in the table for a specific observer. The mapping would be done external (eg in a plugin). But that's theory. In practice it may be better in some case like ours to have the the table load some of the observer directly (and some others may be mapped from external).
 
TableObserverTags::createObserver

Is this a typo and you meant JTable? If not, is TableObserverTags a custom table? Would you mind to make clear, which files and classes in summary must be created and how must they be tight together to introduce each other and to make them work together? 

TableObserverTags is the class defined in /libraries/joomla/table/observer/tags.php. This is the actual observer class which will do the work. There is another one for contenthistory in the same directory. The class is autoloaded in those cases. In your case you have to create your own observer class and require_once it before you use createObserver. But I never created an own, I just figured out how to use the ones for tags and contenthistory :)
Message has been deleted

Clubnite

unread,
Sep 12, 2014, 10:42:21 AM9/12/14
to joomla-de...@googlegroups.com
TableObserverTags is the class defined in /libraries/joomla/table/observer/tags.php.

Nope, it is

class JTableObserverTags extends JTableObserver

Note the leading J. So i assume, you meant

JTableObserverTags::createObserver($this, array('typeAlias' => 'com_content.article'));
Reply all
Reply to author
Forward
0 new messages