2

I can't find any resources explaining how to use the update() method of a .upd file in module development. All the tutorials gloss over it saying things like;

"we don't need this right now, so we just return TRUE"

I do need it - I'd like to add another action into the database without having to reinstall my module, can anybody point me in the right direction please?

3

@naboovallay said it all really, but seeing as I had exactly the code you wanted already open, I figured I'd paste it...

function update($current = '') {
    if ($current == '' OR $current == $this->version)
        return FALSE;

    if (preg_replace("/[^0-9\.]/", '', $current) < '1.1.0') {
        //###   Create ACTion IDs for the relevant functions   ###
        $formActions = array(
            "google_feed"
        );
        foreach ($formActions as $method) {
            $data = array(
                'class' => $this->class,
                'method' => $method
            );
            $this->EE->db->insert('actions', $data);
        }//###   End of foreach
    }

    return TRUE;
}//###   End of update function

Most add-on developers store the version details in a config file in root of the module, so that can be easily read in by the construct...

//###   PHP5 Constructor   ###
function __construct() {
    require PATH_THIRD.'seo/config.php';

    $this->EE =& get_instance();
    $this->version = $config['version'];

...hence the version number of the build we can compare to.

Config.php

<?php
$config = array(
    'name'              => "SEO",
    "class"             => "Seo",
    "version"           => "1.1.0",
    'nsm_addon_updater' => array('versions_xml' => 'http://peteralewis.com/releases/seo/feed')
    );
| improve this answer | |
2

First you need to get the version of your add-on so know if you should run any update functions or not. Replace the "ENTER_YOUR_CURRENT_VERSION_HERE" with the current version of your add-on. You could do this like this:

public function update($current = '')
{
    // Is the current version (from database) lower then the version for your current add-on?
    if ($current < ENTER_YOUR_CURRENT_VERSION_HERE)
    {
        // Do something to update to version X.X.X
    return TRUE;
    }
}

Then you could just keep adding these IF-statements for all version updates. This approach is pretty basic, if you an add-on with multiple version and releases here and there you should make this functions a little better with loading new version updates from flat files or something better. Let's say a user wants to update 3 version in a row, then you want all the updates to run after each other. Then you need a better loop code to execute all updates.

Also when you are done you need the function to return TRUE.

| improve this answer | |
2

There is a version_compare function that can help simplify version comparison conditionals. See this example in the documentation.

| improve this answer | |

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.