OJS OCS OMP OHS

You are viewing the PKP Support Forum | PKP Home Wiki



A hook after the SQL has been executed?

OJS development discussion, enhancement requests, third-party patches and plug-ins.

Moderators: jmacgreg, btbell, michael, bdgregg, barbarah, asmecher

Forum rules
Developer Resources:

Documentation: The OJS Technical Reference and the OJS API Reference are both available from the OJS Documentation page.

Git: You can access our public Git Repository here. Comprehensive Git usage instructions are available on the wiki.

Bugzilla: You can access our Bugzilla report tracker here.

Search: You can use our Google Custom Search to search across our main website, the support forum, and Bugzilla.

Questions and discussion are welcome, but if you have a workflow or usability question you should probably post to the OJS Editorial Support and Discussion subforum; if you have a technical support question, try the OJS Technical Support subforum.

A hook after the SQL has been executed?

Postby rmichael » Wed Aug 06, 2008 5:56 am

Hello,

I've discovered a slight roadbump to my plugin. (I think!)

An example: I register against the hook SuppFileDAO::_updateSuppFile. To process the information being updated, I take the supp_id from the args and get a SuppFile object. However, because my hook is called before the SQL has been executed, the SuppFile object I retrieve doesn't yet have the new information.

As an alternative, somewhere in either args() or params(), I must be able to find all the data that's being put into the DB. However, unwinding all that really seemed backward. Using the object's getXyz() methods is much easier (and obviously less prone to future breakage).

I would like a hook that's called after the SQL has executed and the updated information is in the DB. Following the codepath for _insertSuppFile, I don't see that opportunity. Or, in general, I want my hook to somehow run after DB updates.

Any suggestions? (E.g. I could store the supp_id, then have periodic notifications which poll the DB with the previously stored id's. But that doesn't seem very elegant either.)

Thanks!
rmichael
 
Posts: 113
Joined: Fri Mar 30, 2007 3:32 pm

Re: A hook after the SQL has been executed?

Postby rmichael » Thu Aug 07, 2008 4:39 am

Ok, replying to my own post after a closer reading of DAO::update()

Code: Select all
        function update($sql, $params = false, $callHooks = true, $dieOnError = true) {
                if ($callHooks === true && checkPhpVersion('4.3.0')) {
                        $trace = debug_backtrace();
                        // Call hooks based on the calling entity, assuming
                        // this method is only called by a subclass. Results
                        // in hook calls named e.g. "SessionDAO::_updateSession"
                        $value = null;
                        if (HookRegistry::call($trace[1]['class'] . '::_' . $trace[1]['function'], array(&$sql, &$params, &$value))) {
                                return $value;
                        }
                }

                $this->_dataSource->execute($sql, $params !== false && !is_array($params) ? array($params) : $params);
                if ($dieOnError && $this->_dataSource->errorNo()) {
                        fatalError('DB Error: ' . $this->_dataSource->errorMsg());
                }
                return $this->_dataSource->errorNo() == 0 ? true : false;
        }


It seems I could write my hook to return TRUE, then perform the _dataSource->execute() bits from DAO::insert() in my own code, do my notifications using retrieved objects from the database as I wanted, then set &$value to the same value as the DAO::insert() code (e.g. _dataSource->errorNo() stuff)?

Does that make sense? I'll guess I'll just do it and see. :-)
rmichael
 
Posts: 113
Joined: Fri Mar 30, 2007 3:32 pm

Re: A hook after the SQL has been executed?

Postby rmichael » Thu Aug 07, 2008 9:26 am

Replying to myself again: instead of having my code duplicate the last bit in DAO::insert() (I was worried about having to track changes to that method; and I dislike repetition!), I realized it would be cleaner to call DAO::insert() again, but with callhooks = false.

This has worked nicely I think. However, the remaining problem is the data updated by updateLocaleFields(), and it is all useful information. These updates happen after the initial SQL has executed and there is no hook further in the execution path (in DAO::updateDataObjectSettings, for example).

Suggestions?

Cheers!
rmichael
 
Posts: 113
Joined: Fri Mar 30, 2007 3:32 pm


Return to OJS Development

Who is online

Users browsing this forum: Google [Bot] and 2 guests