Creating an Addon.

I need to create a database field when the add-on is installed, what do I have to do? I have created an "Installer.php" file, created call-back class and methods but cant seem to work.

Thanks
 
I need to create a database field when the add-on is installed, what do I have to do? I have created an "Installer.php" file, created call-back class and methods but cant seem to work.

Thanks


You create a variable to hold the SQL query for you, inside the Installer.php file example:

PHP:
protected static $table = array(
        'createQuery' => 'CREATE TABLE IF NOT EXISTS `xf_my_table` (
                    `mytable_id` int(10) NOT NULL AUTO_INCREMENT,
                    `other_field` VARCHAR( 50 ) NOT NULL ,
                    `other_2_field` VARCHAR( 50 ) NOT NULL ,
                    `other_3_field` VARCHAR( 50 ) NOT NULL ,                   
                    PRIMARY KEY (`mytable_id`)
        ) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;',
        'dropQuery' => 'DROP TABLE IF EXISTS `xf_my_table`'
);

Then, you create two functions, install() and uninstall():

PHP:
public static function install()
    {
        //We get here the instance of the XenForo db.
        $db = XenForo_Application::get('db');
       
        //Tell the db to query our 'createQuery'.
        $db->query(self::$table['createQuery']);
    }


PHP:
public static function uninstall()
    {
        //We get here the instance of the XenForo db.
        $db = XenForo_Application::get('db');
       
        //Tell the db to query our 'dropQuery', because we are uinstalling.
        $db->query(self::$table['dropQuery']);
    }


Then after, just set the two callbacks via the add-on manager and it's done!
 
I just wanted to thank you for this, Lawrence. I found it very informative, and I'm really happy someone wrote something like this. Thanks again. =]
 
I just wanted to thank you for this, Lawrence. I found it very informative, and I'm really happy someone wrote something like this. Thanks again. =]

Thanks. I may look at creating another one that writes to a database. I just need an idea for something simple that will need to store info. As I continue to develop my RP site, I will eventually come across something fairly simple that I want implemented and others may find beneficial, and that I will turn into a tutorial.
 
To an external database, or to XenForo's database? If you want a good educational tutorial, I'd probably suggest something where use of the database is not only necessary, but also helps streamline the process of whatever the addon deals with, especially if it's integrating new options into standard XenForo behavior. It would be a good way to show people how to have their addons work alongside XenForo without messing up the integrity or conventions that XenForo follows. I'm not used to MVC yet, but if there's any help I can offer, don't hesitate to let me know. Good luck. =]
 
Thanks for the offer, though I am getting pretty proficient with XF's data writers. I'll be adding a lot of small-ish code to my site and when I come across one that some here will find useful I'll create the tutorial off that.
 
We can leave this function blank until we create a new options group in the AdminCP. If you are following along with each part, you can turn off this Add-on until we add in the code:

AdminCP home, and uncheck the Add-on Limit Signatures for new Members.

Just wanted to say that you shouldn't turn off the add-on at this point, because if you do, you can't create a new Options Group. You will get an error message that says that you need to turn on the add-on first.
 
I totally forgot to thank you for this thread. This is the thread that taught me how to build stuff for XenForo initially.

Kind of funny looking back now... The first hour of mucking with it, I remember I was like, "Deerrrrr what?" I think I've come a little ways since then. :)
 
Back
Top Bottom