Plugin Installer - Install & Upgrade

Before reading this, you may want to look at the Schema class, as all queries in plugin installer must use this class for database compatibility.


Your plugin might need to store or retrieve data from a table. For that purpose you have to specify the required schema.

Create a folder named 'install' in your plugin directory.

helloworld
|-- helloworld.info.php
|-- helloworld.php
`-- install
    |-- 1.0.php
    |-- 1.1.php
    |-- 2.0.php

Here 1.0.php, 1.1.php & 2.0.php are installation files. There is an installation file for each plugin version. Codoforum uses the file names to determine which file to execute first, and in what order they will be executed.

Note: If you have version 1.0 and are updating to version 1.1, the 1.1.php file will not be executed if there was no 1.0.php file in the 1.0 release. For this reason, it is good practice to have an update file for each version, even if there is no change in that version. You can just use an empty file or a file with a comment line.

Codoforum uses the PHP version_compare function to execute the correct update files. Familiarize yourself with this function as your version strings need to adhere to the conventions of this function.

For example, the install/update files for version 1.3 of this example include:

#1.0.php

    <?php

    Schema::create(PREFIX.'messages', function($table)
    {
        $table->increments('id');
    });

#1.1.php
   <?php
     //empty file since there are no changes to table.

#1.1.1.php
    <?php
    
    Schema::table(PREFIX . 'messages', function($table)
    {
        $table->string('message');
    });

#1.2.php
    <?php
    
    DB::insert('INSERT INTO '.PREFIX.'messages (id,message) VALUES (1,'Hello world!');

#1.2.1.php
    <?php
    
    DB::update('UPDATE '.PREFIX.'messages SET message='Hello again' WHERE id=1');

#1.3.php
    <?php
    
    DB::insert('INSERT INTO '.PREFIX.'messages (id,message) VALUES (2,'How are you ?');

Note: Make sure you use the PREFIX constant before every table name

The PREFIX constant is the database prefix defined at the time of codoforum installation

© CODOFORUM 2024. All Rights Reserved.