Joomla 2.5:如何向全局数据库对象添加自定义查询?

I am upgrading a website from 1.5 to 2.5 which is used by different clients. To make sure to display the correct menues and modules I need to make a custom query in a system plugin when loading the page.

How can I set my query to the global dbo? I need to add a few lines so that the query also gets in the standard query in menu.php for example.

Here is my code:

$db = JFactory::getDbo();

$query = $db->getQuery();

$query->leftJoin("#__menu_types AS mt ON m.menutype = mt.menutype");
$query->leftJoin("#__wl_clientmap as cm on cm.target_id=mt.id AND cm.target_type='mnu'");
if ($this->clientid>0){
        $query->where("cm.client_id=".intval($this->clientid)."");
}
else {
    $query->where("(cm.client_id=0 OR cm.client_id IS NULL)");
}
$db->setQuery($query);

$menu = & JSite::getMenu();

and here is what's in includes/menu.php:

public function load()
{
    // Initialise variables.
    $db     = JFactory::getDbo();

    $app    = JApplication::getInstance('site');
    $query  = $db->getQuery(true);

    $query->select('m.id, m.menutype, m.title, m.alias, m.note, m.path AS route, m.link, m.type, m.level, m.language');
    $query->select('m.browserNav, m.access, m.params, m.home, m.img, m.template_style_id, m.component_id, m.parent_id');
    $query->select('e.element as component');
    $query->from('#__menu AS m');
    $query->leftJoin('#__extensions AS e ON m.component_id = e.extension_id');
    $query->where('m.published = 1');
    $query->where('m.parent_id > 0');
    $query->where('m.client_id = 0');
    $query->order('m.lft');

Sadly my custom query does not appear on menu.php's query. How can I make sure that it gets in?

You can hijack the database object the same way Joomfish does, roughly it works like this in a system plugin

$db = & JFactory::getDBO();
$db = new JFDatabase($options);

Then all queries go through your object and you can pass anything you don't want to change on to the normal database layer.

Have a look at jfrouter plugin in Joomfish to get some more ideas.