1

I found by Joomla 3.x, that I can use an install script something like this:

class Com_ExampleInstallerScript
{
    public function install($adapter) 
    {
        // create tables
    }

    public function uninstall($adapter) 
    {
        // remove tables
    }

    public function update($adapter) 
    {
        // alter tables
    }

    public function preflight($route, $adapter) 
    {
        // stop service, probably creating a lock file
    }

    public function postflight($route, $adapter) 
    {
        // start service, probably removing a lock file
    }
}

Everything is fine except the update part. I guess this runs when I already have installed an older version and try to install the new version. If so, then how can I get the version number of the old version? I don't want to use upgrade SQL files, so that's not the answer.

1

You can get the old version like so:

class Com_ExampleInstallerScript
{
    protected $release = '';

    protected $paramTable = '#__modules';

    protected $extension = 'mod_mymodule';

    public function preflight($type, $parent)
    {
        // Module manifest file version
        $this->release = $parent->get('manifest')->version;

        // Abort if the module being installed is not newer than the currently installed version
        if (strtolower($type) == 'update')
        {
            $manifest   = $this->getItemArray('manifest_cache', '#__extensions', 'element', JFactory::getDbo()->quote($this->extension));
            $oldRelease = $manifest['version'];

            if (version_compare($this->release, $oldRelease, '<'))
            {
                JFactory::getApplication()->enqueueMessage(JText::sprintf('YOU_CANT_UPDATE_TO_A_LOWER_VERSION', $oldRelease, $this->release), 'error');
                return false;
            }
            if (version_compare($oldRelease, $this->release, '<'))
            {
                // If old version is the same as or less than 1.0.1
                if (version_compare($oldRelease, '1.0.1', '<='))
                {
                    // DO something here
                }
            }
        }
        return true;
    }
}

I've added a few comment to assist. Hope this helps

  • Thanks! I hoped that I don't have to read it from the manifest cache, but ok. :D I don't need the comments, it's just noise, good quality code is enough. :-) – inf3rno Feb 5 '17 at 23:52
  • I have a 40 min limit on 2 questions. :S Can we make things faster? Is there a way to use query templates (aka. pdo prepared statements) by JDatabase? – inf3rno Feb 6 '17 at 0:02
  • ask a new question please – Lodder Feb 6 '17 at 0:09
  • Ok, I'll wait out the 40 mins then. :-) – inf3rno Feb 6 '17 at 0:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.