To start we must know what are the fields that we use to read/write in the database. That is, the fields that will store the information we want to read later.
I'll start setting the table name, fields and types of each:
simple_id -> The autoincrement field of each row.
simple_text -> This is the field that will contain our written text. Type: varchar; Lenght: 200;
simple_date -> This is the date when we write the text. Type: INT; Lenght: 10;
- The more precise you define the types of each field in your table, the better the performance.
Step 2 - Creating the installer
When creating an add-on for XenForo you should follow a standard structure, both in the creation of folders such as the naming of the classes.
All files in our add-on should be created in the folder forumroot/library/. This folder will also contain all other add-ons you have installed on your forum.
First of all, let's create a folder for our add-on. "But where?" You must be wondering. Inside the folder forumroot/library/. So start by creating a folder with the name of our add-on: "SimpleText".
This is the skeleton of the folder (note that I'm just listing the folder XenForo for reference):
----SimpleText -> our new folder!
Now that we've created the folder, we must create a file called Installer.php that will contain two methods: install() that creates the table and uninstall() it will delete this table (when you uninstall).
-----Installer.php -> Our new file!
We have our first folder and file in PHP. But what do we do with it? See the next step.
A. The Installer.php will be our installation file for our add-on. You can put whatever you want. "Installme.php", "MyFileInstall.php" or previously "ThisFileDoNotHaveAName.php" (not recommended). But for better understanding, simply use "Installer.php", so we know what it is about this file. By creating an add-on, we have the option of selecting a class for installation (and method) and a class to uninstall (and method). So every time someone install/uninstall your add-on, this class / method is called. As this simple add-on uses a table in the database to record and read the data, we will use this property of the add-on to tell XenForo that: "XenForo when installing my add-on please find a class and call the method SimpleText_Installer::install(), ok?"
Step 3 - Installer.php
Installer.php within the file will have two functions: install() and uninstall(). Respectively, the two will be used to install and uninstall our add-on.
So, open the file Installer.php and we will begin to develop by creating a class:
Yes! Take a look:
SimpleText -> The name of our directory.
Installer -> The name of our file.
Therefore, we will always follow this pattern. If we had a structure like this:
---SimpleText -> folder
----Installer -> folder
-----Installer.php -> file
How would the class name of our file Installer.php?
SimpleText -> Folder!
Installer -> Folder!
Installer -> File!
So I always follow this structure. Over the next file you will get more used to it.
Back to our installation file, we will now create a variable that will contain the definitions of our table, along with the fields you want to create and the types of each. For this, we create a variable called "$table". This variable holds a SQL CREATE command who will be responsible for creating the table in our database.
You may notice that the variable $table is actually an array with two keys:
createQuery -> SQL command to create the table.
dropQuery -> SQL command to delete the table.
Let's dissect the key createQuery SQL command:
CREATE TABLE IF NOT EXISTS - Create the table if it does not exist. If it exists, skip this step and proceed to the next (if any). For details of the condition IF NOT EXISTS, click here.
xf_simple_text - This is the name of the table we want to create the database.
`simple_id` INT ( 10 ) - The autoincrement field of this table.
`simple_text` VARCHAR ( 200 ) - First we'll create the field is called 'simple_text'. The field that will store the text we have written and then saved in the table. The type it is VARCHAR and the lenght is 200.
`simple_date` INT UNSIGNED ( 10 ) - This field will store the date you sent the text to the database. Will be changed every time you write a new text and save the table. It is the type INT UNSIGNED the lenght is 10.
ENGINE = InnoDB - We will use a Inno DB table. More information here.
SET utf8 COLLATE utf8_general_ci - More information here.
And the key dropQuery has the following command:
DROP TABLE IF EXISTS `xf_simple_text` - This will delete the table of our database, if it exists. If not, nothing will be done. More information about the DROP TABLE here.
Everything seen here? Ok, now we have our two queries for creating and deleting the table, we create two function that will run these two SQL statements.
This will be our method for installing the add-on. The following code:
This is the method to uninstall our add-on. The following code:
And this is the final code:
Step 4 - The Model
The Model is responsible for reading data from our table. A brief explanation:
methods for common actions. E.G.: When we are accessing a thread in our forum, the model is responsible for bringing the content of the topic, by querying the database.
-----Model - new!
--------SimpleText.php - new!
Open the file Model/SimpleText.php and let's give a name to the class:
With the file opened, we will create a method to get a row in the database:
Now, the next method will get ALL the rows.
Step 5 - The DataWriter
The DataWriter will be responsible for writing our data into our table. Most often, when you develop an add-on, you will need a DataWriter. Then, go to work.
What is a DataWriter?
-----DataWriter - new!
--------SimpleText.php - new!
So, open the SimpleText.php file and let's give the file a class name:
----------------- // -----------------
And the last one, the method to get our Model, so we can use it inside this DataWriter: