A CRUD in five minutes with BF
Paul Hodel

If you are new to Bossanova Framework, this article is going to be very useful from a practical point of view to present you the fundamentals of the platform. So, the following instructions is to give developers the basic instructions on how to create the simplest CRUD module considering common best practices.

If don't have BF installed yet we recommend to read our article From zero to Bossanova Framework with Bitnami. So without further due, the idea is create a countries CRUD, so please follow the instructions.
 

Database

Please create the table:

CREATE TABLE countries
(
  country_id serial NOT NULL,
  country_name character varying(255) NOT NULL,
  country_status smallint,
  CONSTRAINT countries_pkey PRIMARY KEY (country_id)
)

Model

Please create a new model file to: /models/Countries.class.php

<?php

namespace models;

use bossanova\Model\Model;

class Countries extends Model
{
    // This is just a demo for the grid
    public function grid ()
    {
        // Get all countries
        $result = $this->database->table("countries")
            ->column("country_id, country_name, country_status")
            ->argument(1, "country_status", 1)
            ->select()
            ->execute();

        // Format grid and return the data
        return $this->gridFormat($result);
    }
}

Module

Please create the following folder structure:

/modules/Countries
/modules/Countries/views
/modules/Countries/controllers

 

Please create a new PHP class: /modules/Countries/Countries.class.php

<?php

namespace modules\Countries;

use bossanova\Module\Module;

class Countries Extends Module
{
    public function __default ()
    {
    }
    
    public function grid ()
    {
        // Load model
        $countries = new \models\Countries;

        // Load the data
        $grid = $countries->grid();

        // return the array in a json format
        return $this->json_encode($grid);
    }
}

View

Create a new plain HTML view file: /modules/Countries/views/countries.html

<form id='countries_form'>
<table cellspacing='10' class='adminForm'>
<tr>
<td>

    <table cellpadding="4" cellspacing="0">
    <tr><td><label>Country ID</label><br><input type="text" name="country_id" style="width:280px;" readonly="readonly"></td></tr>
    <tr><td><label>Country Name</label><br><input type="text" name="country_name" style="width:280px;"></td></tr>
    <tr><td><label>Country Status</label><br>
        <select type="text" name="country_status" style="width:280px;">
        <option value="1">Enabled</option>
        <option value="0">Disable</option></select></td></tr>
    </table>

</tr>
<tr>
<td>
    <input type="button" value="Save" name="save" style="width:90px;">
    <input type="reset" value="New" style="width:90px;">
</td>
</tr>
</table>
</form>

<div style="padding:10px;">
    <div class="grid" id="countries_form_grid"></div>
</div>

<script>
$(document).ready(function() {
    $('#countries_form').form({ url: bossanova_url + 'countries', primarykey:'country_id' });

    $('#countries_form_grid').grid({
        url: bossanova_url + 'countries/grid',
        columns:[
            {title:'Id',width:'40', search:'1'},
            {title:'Name',width:'120', search:'1'},
            {title:'Status',width:'100', search:'2', search_combo:{'1':'Enabled','0':'Disabled'}},
            {title:'',width:'60'}
            ],
        actions:[
            {title:'Open',icon:'img/open.png',click:'countries_open'},
            {title:'Delete',icon:'img/delete.png',click:'countries_delete'}
            ]
    });
});

function countries_open (id)
{
    $('#countries_form').form('open', id);
}

function countries_delete (id)
{
    if (confirm('Are you sure?')) {
        $('#countries_form').form('delete', id, function () {
            $('#countries_form_grid').grid('refresh');
        });
    }
}
</script>

Testing

Now to test your new module, you can call the URL below:
http://localhost/countries

To see it live:
http://www.bossanova-framework.com/countries
 

Behind the scenes

Basically, all RESTful requests from the frontend are implemented by the Bossanova UI FORM Plugin [public/componentes/bossanova/js/jquery.form.js]. So the following actions will be performed via ajax.

http://localhost/countries/select
http://localhost/countries/insert
http://localhost/countries/update
http://localhost/countries/delete

Those methods are natively implemented by the Module Library and inhetered by your Countries class to automatically handle the requests. Of course, these methods can be overwrite to fits specific needs. If you would like more information about how BF handles restful requests please check the documentation of Fundalmentals of Bossanova.