Models

Introduction

Although, you can use the Database Library directly from controllers for table operations, the Model Library usage would help to keep a distinct layer in your application for data operations and business rules. This practice is recommended and brings several benefits including, code reuse and helps to keep your application in line with the best developement practises.

Each model class in your application represents a table in your database. Thus, each model would have some table properties and some native methods inherited from the Model Library to interect with the table and its data. Additionally, developers can add their own custom methods and business rules to the class. So, these methods will be centralized and can be reused accross all modules and controllers within the application.

 

Creating a new model

All models in your application will be available in the models folder, and the basic structure will be for example:

models/Clients.class.php

<?php

namespace models;

use Bossanova\Model\Model;

class Clients extends Model
{
    // Table configuration
    public $config = array('tableName'  => 'clients',
                           'primaryKey' => 'client_id',
                           'sequence'   => 'clients_client_id_seq',
                           'recordId'   => 0);
}
NOTE: If the $config is not defined, the Model Library will try to populate that automatically.

 

Basic operations

Get a record as an array

Please consider the following example:

<?php

namespace modules\Clients;

use Bossanova\Module\Module;

class Clients Extends Module
{
    public function get ($id)
    {
        $model = new \models\Clients;
        $row = $model->getById(id);

        return json_encode($row);
    }
}

 

Get and edit a record

Please consider the following example:

<?php

namespace modules\Clients;

use bossanova\Module\Module;

class Clients Extends Module
{
    public function updateName ($name)
    {
        $model = new \models\Clients;
        $client = $model->get(12);
        $client->client_name = $name;
        $client->save();
    }
}

Delete a record

Please consider the following example:

<?php

namespace modules\Clients;

use bossanova\Module\Module;

class Clients Extends Module
{
    public function delete ()
    {
        $model = new \models\Clients;
        $model->delete(12);
    }
}

 

Runtime model

The runtime model is the ability to create a instance of the Model Library with all its native methods available on the fly, without creating a physical class. So, please consider the following example:

<?php

namespace modules\Clients;

use bossanova\Module\Module;
use bossanova\Database\Database;

class Clients Extends Module
{
    public function __default()
    {
        // Log in to the database
        $db = Database::getInstance("pgsql_instance_name", array("pgsql", "localhost", "db_username_pg", "db_passowrd_pg", "db_name_pg"));

        // Create the model on the fly, load one record, edit the product data and save
        $products = $db->find("products", 65);
        $products->product_name = "Product name";
        $products->product_price = 22.00;
        $products->save();

        // If table clients exist you can create the object                  
        $clients = $db->clients;
        $clients->client_name = "Roger Walters";
        $clients->client_status = 1;
        $clients->save();
    }
}

Standalone mode

If you are not using the BF but would like to use the Database Library on your own projects, please consider the example below:

<?php

// Minimum full standalone usage
require "vendor/bossanova/Database/Database.class.php";
require "vendor/bossanova/Model/Model.class.php";

// First time call, send connection information
$db = Database::getInstance("pgsql_instance_name", array("pgsql", "localhost", "db_username_pg", "db_passowrd_pg", "db_name_pg"));

// Create the model on the fly, load one record, edit the product data and save
$products = $db->model("products")->getById(65);
$products->product_name = "Product name";
$products->product_price = 22.00;
$products->save();

// Adding a new record
$clients = $db->model("clients"); // Or just $db->clients
$clients->client_name = "Roger Walters";
$clients->client_status = 1;
$clients->save();