Archived talk

Developing a MVC Component/Adding an install-uninstall-update script file

From Joomla! Documentation

I vote for some additional code in the preflight method, since everybody should check against a suitable version prior to install. Here is the code from the v1.6 preflight method example. I will wait for a week, before adding it to the page. Maybe there are suggestions for V2.5, that should be merged into this code, before we publish it. --gorgonz 06:05, 26 May 2012 (CDT)

First we must add some private members to the com_helloWorldInstallerScript class

	 * The release value to be displayed and checked against throughout this file.
	private $release = '1.0';
         * Find mimimum required joomla version for this extension. It will be read from the version attribute (install tag) in the manifest file
        private $minimum_joomla_release = '2.5.0';

And here is the implementation

	 * $parent is the class calling this method.
	 * $type is the type of change (install, update or discover_install, not uninstall).
	 * preflight runs before anything else and while the extracted files are in the uploaded temp folder.
	 * If preflight returns false, Joomla will abort the update and undo everything already done.
	function preflight( $type, $parent ) {

		// this component does not work with Joomla releases prior to 2.5
		// abort if the current Joomla release is older
		$jversion = new JVersion();
                // Extract the version number from the manifest. This will overwrite the 1.0 value set above 
                // Find mimimum required joomla version
		if( version_compare( $jversion->getShortVersion(), $this->minimum_joomla_release, 'lt' ) ) {
			Jerror::raiseWarning(null, 'Cannot install com_helloWorld update in a Joomla release prior to '.$this->minimum_joomla_release);
			return false;
		// abort if the component being installed is not newer than the currently installed version
		if ( $type == 'update' ) {
			$oldRelease = $this->getParam('version');
			$rel = $oldRelease . ' to ' . $this->release;
			if ( version_compare( $this->release, $oldRelease, 'le' ) ) {
				Jerror::raiseWarning(null, 'Incorrect version sequence. Cannot upgrade ' . $rel);
				return false;
		else { $rel = $this->release; }
		echo '<p>' . JText::_('COM_HELLOWORLD_UPDATE_PREFLIGHT_' . $type . ' ' . $rel) . '</p>';

Postflight - uninstall[edit]

Testing in joomla 2.5.7 it seems that postflight does not handle "uninstall" as indicated here. This doc:!1.6_-_Part_3 seems to have the correct definition of postflight.

Should we update the article?