0

I'm using postflight to try and set my module's module assignment to 'All pages' and here's the code.

    // Module assignment 'All pages'
    if (strtolower($type) === 'install') 
    {
        jimport( 'joomla.application.module.helper' );
        $module = JModuleHelper::getModule('mod_backtotop');
        $moduleid = $module->id;

        $db = JFactory::getDbo();
        $query = $db->getQuery(true);

        $fields = array(
            $db->quoteName('moduleid') . ' = ' . $db->quote($moduleid),
            $db->quoteName('menuid') . ' = 0'
        );

        $query->insert($db->quoteName('#__modules_menu'))->set($fields);

        $db->setQuery($query);   
        $db->execute();     
    }

The code is from another question's answer kindly provided with thanks from @Lodder

When I first installed the module, module assignment didn't change and still showed 'No pages'. So, I changed the code to the following knowing what the next module ID will be when installed again.

    // Module assignment 'All pages'
    if (strtolower($type) === 'install') 
    {
        jimport( 'joomla.application.module.helper' );
        $module = JModuleHelper::getModule('mod_backtotop');
        $moduleid = $module->id;

        $db = JFactory::getDbo();
        $query = $db->getQuery(true);

        $fields = array(
            $db->quoteName('moduleid') . ' = 129',
            $db->quoteName('menuid') . ' = 0'
        );

        $query->insert($db->quoteName('#__modules_menu'))->set($fields);

        $db->setQuery($query);   
        $db->execute();     
    }

This worked perfectly.

However, when I reinstalled the module using the top piece of code again I get an installation error.

Warning

Duplicate entry '0-0' for key 'PRIMARY'

Why won't the code at the top work?

Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.