Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Piwik_Query() throws exception when used in a Tracker event hook #882

Closed
anushr opened this Issue · 9 comments

4 participants

Anush Ramani Anthon Pang Matthieu Aubry Anonymous Piwik user
Anush Ramani

I get the following exception when trying to use Piwik_Query() from within my plugin:

No entry is registered for key 'db'
#0 C:\\wamp\\www\\piwik\\core\\PluginsFunctions\\Sql.php(24): Zend_Registry::get(\'db\')
#1 C:\\wamp\\www\\piwik\\plugins\\MyPlugin\\MyPlugin.php(136): Piwik_Query(\'INSERT INTO my...\', Array)
#2 [internal function]: Piwik_MyPlugin->myHandler(Object(Piwik_Event_Notification))
#3 C:\\wamp\\www\\piwik\\libs\\Event\\Dispatcher.php(284): call_user_func_array(Array, Array)
#4 C:\\wamp\\www\\piwik\\core\\PluginsManager.php(491): Event_Dispatcher->postNotification(Object(Piwik_Event_Notification), true, false)
#5 C:\\wamp\\www\\piwik\\core\\Tracker\\Action.php(194): Piwik_PostEvent(\'Tracker.Action....\', Object(Piwik_Tracker_Action), Array)
#6 C:\\wamp\\www\\piwik\\core\\Tracker\\Visit.php(142): Piwik_Tracker_Action->record(\'23\', \'16\', 88)
#7 C:\\wamp\\www\\piwik\\core\\Tracker.php(64): Piwik_Tracker_Visit->handle()
#8 C:\\wamp\\www\\piwik\\piwik.php(59): Piwik_Tracker->main()
#9 {main}

Here is my hook registration:

function getListHooksRegistered()
{
    $hooks = array(
        'Tracker.Action.record' => 'myHandler',
        'Menu.add' => 'addMenu',
    );
    return $hooks;
}

And my handler implementation:

public function myHandler($notification)
{
    $trackerAction =& $notification->getNotificationObject();
    $idAction = $trackerAction->getIdAction();

    try
    {
        Piwik_Query('INSERT INTO my_table (idaction, myvalue) VALUES (?,?);', array($idAction, 0));
    }
    catch(Exception $e)
    {
        self::log($e->getMessage());
        self::log($e->getTraceAsString());
    }
} 

--- WORKAROUND ---

A workaround is available, but this deviates from the published "Security Checklist" which recommends the use of Piwik_Query().

Piwik_Tracker::getDatabase()->query($query, $params);
Anthon Pang
Collaborator

That's the intended behaviour. To keep it lightweight, the tracker (and tracker hooks) don't use Zend_Db.

Your "workaround" is the correct approach for a tracker hook.

Matthieu Aubry
Owner

I created #884 to list all missing documentation about functions to use.
please add a comment if you find more! we would like to make all docs up to date.

Anonymous Piwik user

Can you please explain what the workaround is or provide a link that does? Thanks

Anonymous Piwik user

Ha, nevermind. I don't know why I didn't see that before.

Piwik_Tracker::getDatabase()->query($query, $params);

Matthieu Aubry
Owner

reopening and setting as documentation update

Anthon Pang
Collaborator

If I'm not mistaken, I think [1970]+[2012] now allows Piwik_Query() to be called from a Tracker event hook.

Anthon Pang
Collaborator

Replying to vipsoft:

Whether or not it works depends on whether or not getCacheWebsiteAttributes() has already been called. For example, it wouldn't work for a listener on the 'Tracker.Visit.isExcluded' hook.

Matthieu Aubry
Owner

right, I think the idea would be to make the function call work in all cases, or document that Tracker functions must use Piwik_Tracker::getDatabase()->query($query, $params);

Anthon Pang
Collaborator

(In [2054]) fixes #882 - Piwik_Query() can now be used by tracker plugins;
fixes #1292 - remove (deprecated/unused) Piwik_Quote() since we use Zend_Db for prepared statements; add Piwik_FetchRow() for completeness

Anush Ramani anushr added this to the Piwik 0.6 milestone
Anthon Pang robocoder was assigned by anushr
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.