@anush opened this Issue on July 26th 2009

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

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

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.

@mattab commented on July 27th 2009 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.

@anonymous-piwik-user commented on March 15th 2010

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

@anonymous-piwik-user commented on March 15th 2010

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

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

@mattab commented on March 16th 2010 Member

reopening and setting as documentation update

@robocoder commented on March 31st 2010 Contributor

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

@robocoder commented on April 4th 2010 Contributor

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.

@mattab commented on April 6th 2010 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);

@robocoder commented on April 6th 2010 Contributor

(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

This Issue was closed on August 31st 2010
Powered by GitHub Issue Mirror