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

anushr opened this Issue Jul 26, 2009 · 9 comments

4 participants


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();

        Piwik_Query('INSERT INTO my_table (idaction, myvalue) VALUES (?,?);', array($idAction, 0));
    catch(Exception $e)


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);

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.

Piwik Open Source Analytics member

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.


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


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

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

Piwik Open Source Analytics member

reopening and setting as documentation update


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


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.

Piwik Open Source Analytics member

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);


(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

@anushr anushr added this to the Piwik 0.6 milestone Jul 8, 2014
@robocoder robocoder was assigned by anushr Jul 8, 2014
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment