Opened 5 years ago

Closed 4 years ago

Last modified 4 years ago

#882 closed Bug (fixed)

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

Reported by: anush Owned by: vipsoft
Priority: major Milestone: Piwik 0.6
Component: Core Keywords:
Cc: Sensitive: no


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

Change History (13)

comment:1 Changed 5 years ago by vipsoft (robocoder)

  • Resolution set to invalid
  • Status changed from new to closed

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.

comment:2 Changed 5 years ago by matt (mattab)

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.

comment:3 Changed 4 years ago by abowman

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

comment:4 Changed 4 years ago by abowman

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

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

comment:5 Changed 4 years ago by matt (mattab)

  • Component changed from Core to Documentation
  • Milestone changed from Piwik 0.4.3 to 3 - Piwik 0.8 - A Web Analytics platform
  • Sensitive unset

reopening and setting as documentation update

comment:6 Changed 4 years ago by matt (mattab)

  • Resolution invalid deleted
  • Status changed from closed to reopened

comment:7 follow-up: Changed 4 years ago by vipsoft (robocoder)

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

comment:8 in reply to: ↑ 7 Changed 4 years ago by vipsoft (robocoder)

  • Status changed from reopened to new

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.

comment:9 Changed 4 years ago by matt (mattab)

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

comment:10 Changed 4 years ago by vipsoft (robocoder)

  • Milestone changed from 3 - Piwik 0.8 - A Web Analytics platform to 1 - Piwik 0.6
  • Owner set to vipsoft

comment:11 Changed 4 years ago by vipsoft (robocoder)

  • Resolution set to fixed
  • Status changed from new to closed

(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

comment:12 Changed 4 years ago by vipsoft (robocoder)

  • Component changed from Documentation to Core

comment:13 Changed 4 years ago by vipsoft (robocoder)

  • Keywords database removed
Note: See TracTickets for help on using tickets.