Oxwall MySQL Database (OWSQL101): How to Add, Retrieve or Delete Values in Oxwall Config Database

Oxwall MySQL Database (OWSQL101): How to Add, Retrieve or Delete Values in Oxwall Config Database

OWSQL101:

This tutorial (or course, if you like) will guide you on how to add, retrieve and manipulate data stored in Oxwall config SQL database.

Oxwall SQL

If you are asking the same question as the user above – picture taken from the Oxwall forum – I have done my best to put together a simple tutorial that will guide you on how to accomplish just that.

What you need?

Depending on how you want to use this tutorial, you may want to glance through the Oxwall plugin crash course and quickly download the Oxwall Plugin Skeleton. This two items will prepare you on how to structure your plugin (I’m assuming you are developing a plugin).

Finally, get your code editor ready and dive into the next paragraph.

Structure of Oxwall Config

The Oxwall config values are stored in a MySQL database table named ow_base_config. This table contains all the Oxwall key information such as your site name, current theme, site email, etc. This is also the table we will be storing our custom config values.

To avoid conflicts and duplicate config names, each configuration is stored with its plugin key.

Check if Config Exists

Before adding a config record to the table, you can check if the config already exists using the configExists('string','string') function. This function returns true if the config exist or false if it doesn’t exist.

Example:

if (!OW::getConfig()->configExists('plugin_key', 'field_name')) {
    //Configuration does not exist. Add new config or do something else
}

The above code makes sure that the config ‘field_name’ belonging to a plugin with the key ‘plugin_key’ does not exist on the Oxwall config MySQL database before adding a new config (or some action).

Adding a Config to Oxwall MySQL Database

Adding items to the database should be performed on plugin installation. This section should be done on yourplugin/install.php file.

if (!OW::getConfig()->configExists('plugin_key', 'field_name')) {//Make sure config does not exist
    OW::getConfig()->addConfig('plugin_key', 'field_name', 'default_value');//Add this config.
}

Collecting Values using Oxwall Form

This part of the coding should be done on the page where users are suppose to input config values (for the config you added earlier). The example will create a text input box. The content of the input box will be stored in ‘field_name’ in ow_base_config.

PHP (backend) code:

$form = new Form('form_name');//An instance of form named 'form_name'. Give your form a name

$foo = new TextField('field_name');//Create instance of TextField input for 'field_name'
$foo->setLabel('foo_name');//Set input label name
$foo->setHasInvitation(true);//Do you want to show placeholder?
$foo->setInvitation('foo_example');//Placeholder text
$foo->setDescription('foo_desc');//Input description
$foo->addElement($foo);

$fooSave = new Submit('save');
$fooSave->setValue('fooSave');//Submit button text
$form->addElement($fooSave);

$this->addForm($form);

Save Form Values to Oxwall Config Database

if ( OW::getRequest()->isPost() && $form->isValid($_POST) ){//Check if form is submitted and valid
    $data = $form->getValues();
    //Save form values to field_name in db
    OW::getConfig()->saveConfig('plugin_key', 'field_name', trim($data['field_name']));
    //Display some fancy success message and redirect (optional)
    OW::getFeedback()->info('Some success msg');
    $this->redirect();
}

HTML (frontend) code:

{form name='form_name'}
    <table class='ow_table_1'>
        <tr class='ow_alt1'>
            <td class='ow_label'>{label name='field_name'}</td>
            <td class='ow_value'>{input name='field_name'}{error name='field_name'}</td>
            <td class='ow_desc'>{desc name='field_name'}</td>
        </tr>
    </table>
    <div class="clearfix ow_submit ow_smallmargin">
        <div class="ow_right">
            {submit name='save' class='ow_button ow_ic_save'}
        </div>
    </div>
{/form}

Pay specific attention to ‘form_name’ and ‘field_name’. field_name is the name of your database config and form_name, of course, is the name of your form (just give it any name).

Retrieve Config Value from MySQL database

Example:

$dbFoo = OW::getConfig()->getValue('plugin_key', 'field_name');

Modify Oxwall Config Values

Example:

OW::getConfig()->saveConfig('plugin_key', 'field_name','new_value');

Recieve Free Email Updates!

Leave a Reply