2017 © Pedro Peláez
 

library wildfire

Query Builder wrapper for the Codeigniter framework.

image

rougin/wildfire

Query Builder wrapper for the Codeigniter framework.

  • Thursday, May 17, 2018
  • by rougin
  • Repository
  • 2 Watchers
  • 9 Stars
  • 275 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 7 Versions
  • 5 % Grown

The README.md

Wildfire

Latest Version on Packagist ![Software License][ico-license] Build Status ![Coverage Status][ico-coverage] Total Downloads, (*1)

Wildfire is a simple wrapper for the Query Builder Class based on Codeigniter 3. It is inspired from the Eloquent ORM of Laravel., (*2)

Installation

Install Wildfire via Composer:, (*3)

``` bash $ composer require rougin/wildfire, (*4)


## Basic Usage ### Prerequisites Create a sample database table to be used (e.g., `users`): ``` sql -- Import this script to a SQLite database CREATE TABLE users ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name TEXT NOT NULL, age INTEGER NOT NULL, gender TEXT NOT NULL, accepted INTEGER DEFAULT 0 ); INSERT INTO users (name, age, gender) VALUES ('Rougin', 20, 'male'); INSERT INTO users (name, age, gender) VALUES ('Royce', 18, 'male'); INSERT INTO users (name, age, gender) VALUES ('Mei', 19, 'female');

Then configure the composer_autoload option in config.php:, (*5)

``` php // ciacme/application/config/config.php, (*6)

/* |-------------------------------------------------------------------------- | Composer auto-loading |-------------------------------------------------------------------------- | | Enabling this setting will tell CodeIgniter to look for a Composer | package auto-loader script in application/vendor/autoload.php. | | $config['composer_autoload'] = TRUE; | | Or if you have your vendor/ directory located somewhere else, you | can opt to set a specific path as well: | | $config['composer_autoload'] = '/path/to/vendor/autoload.php'; | | For more information about Composer, please visit http://getcomposer.org/ | | Note: This will NOT disable or override the CodeIgniter-specific | autoloading (application/config/autoload.php) */ $config['composer_autoload'] = DIR . '/../../vendor/autoload.php';, (*7)


> [!NOTE] > The value of `composer_autoload` should be the `vendor` directory (e.g., `ciacme/vendor/autoload.php`). Next is to extend the model (e.g., `User`) to the `Model` class of `Wildfire`: ``` php // ciacme/application/models/User.php use Rougin\Wildfire\Model; class User extends Model { }

``` php // ciacme/application/controllers/Welcome.php, (*8)

// Loads the database connection $this->load->database();, (*9)

// Enables the inflector helper. It is being used to // determine the class or the model name to use based // from the given table name from the Wildfire. $this->load->helper('inflector');, (*10)

// Loads the required model/s $this->load->model('user');, (*11)


### Using `Wildfire` ### With `CI_DB_query_builder` After configuring the application, the `Wildfire` class can now be used for returning results from the database as `Model` objects: ``` php // ciacme/application/controllers/Welcome.php use Rougin\Wildfire\Wildfire; // Pass the \CI_DB_query_builder instance $wildfire = new Wildfire($this->db); // Can also be called to \CI_DB_query_builder $wildfire->like('name', 'Royce', 'both'); // Returns an array of User objects $users = $wildfire->get('users')->result();

With CI_DB_result

Aside from using methods of Wildfire, raw SQL queries can also be converted to its Model counterpart:, (*12)

``` php // ciacme/application/controllers/Welcome.php, (*13)

use Rougin\Wildfire\Wildfire;, (*14)

$query = 'SELECT p.* FROM post p';, (*15)

// Create raw SQL queries here... $result = $this->db->query($query);, (*16)

// ...or even the result of $this->db->get() $result = $this->db->get('users');, (*17)

// Pass the result as the argument $wildfire = new Wildfire($result);, (*18)

// Returns an array of User objects $users = $wildfire->result('User');, (*19)


## Properties of `Model` class The `Model` class provides the following properties that helps writing clean code and the said properties also conforms to the properties based on `Eloquent ORM`. ### Casting attributes Updating the `$casts` property allows the model to cast native types to the specified attributes: ``` php // ciacme/application/models/User.php class User extends \Rougin\Wildfire\Model { /** * The attributes that should be cast to native types. * * @var array */ protected $casts = array('accepted' => 'boolean'); }

Without native casts, (*20)

``` json { "id": 1, "name": "Rougin", "age": "20", "gender": "male", "accepted": "0", }, (*21)


**With native casts** ``` json { "id": 1, "name": "Rougin", "age": "20", "gender": "male", "accepted": false, }

Notice that the value of accepted was changed from string integer ('0') into native boolean (false). If not specified (e.g. age field), all values will be returned as string except the id field (which will be automatically casted as native integer, also if the said column exists) by default., (*22)

Hiding attributes

To hide attributes for serialization, the $hidden property can be used:, (*23)

``` php // ciacme/application/models/User.php, (*24)

class User extends \Rougin\Wildfire\Model { /** * The attributes that should be hidden for serialization. * * @var array */ protected $hidden = array('gender'); }, (*25)


**Without hidden attributes** ``` json { "id": 1, "name": "Rougin", "age": "20", "gender": "male", "accepted": "0", }

With hidden attributes, (*26)

``` json { "id": 1, "name": "Rougin", "age": "20", "accepted": "0", }, (*27)


In this example, the `gender` field was not included in the result. ### Visible attributes Opposite of the `$hidden` property, the `$visible` property specifies the fields to be visible in the result: ``` php // ciacme/application/models/User.php class User extends \Rougin\Wildfire\Model { /** * The attributes that should be visible for serialization. * * @var array */ protected $visible = array('gender'); }

Without visible attributes, (*28)

``` json { "id": 1, "name": "Rougin", "age": "20", "gender": "male", "accepted": "0", }, (*29)


**With visible attributes** ``` json { "gender": "male" }

From the example, only the gender field was displayed in the result because it was the only field specified in the $visible property of the User model., (*30)

Using timestamps

Similar to Eloquent ORM, Wildfire enables the usage of timestamps by default:, (*31)

``` php // ciacme/application/models/User.php, (*32)

class User extends \Rougin\Wildfire\Model { /** * Allows usage of timestamp fields ("CREATED_AT", "UPDATED_AT"). * * @var boolean */ protected $timestamps = true; }, (*33)


When enabled, it will use the constants `CREATED_AT` and `UPDATED_AT` for auto-populating them with current timestamps. To modify the names specified in the specified timestamps, kindly create the specified constants to the model (e.g., `User`): ``` php // ciacme/application/models/User.php class User extends \Rougin\Wildfire\Model { /** * The name of the "created at" column. * * @var string */ const CREATED_AT = 'created_at'; /** * The name of the "updated at" column. * * @var string */ const UPDATED_AT = 'updated_at'; }

[!NOTE] Auto-populating of timestamps in the specified constants is used in WritableTrait., (*34)

Customizing attributes

When accessing an attribute through the model, Wildfire uses the same mechanism as from Eloquent ORM to return the requested attribute from the $attributes property. To customize the output of an attribute (e.g., converting an attribute to a date format), add a method inside the Model with a name format get_[ATTRIBUTE]_attribute:, (*35)

``` php // ciacme/application/models/User.php, (*36)

class User extends \Rougin\Wildfire\Model { /** * @return string */ public function get_created_at_attribute() { return date('d F Y H:i:sA', strtotime($this->attributes['created_at'])); } }, (*37)


After creating a method for the specified attribute, the `Model` class will call the said method (e.g., `get_created_at_attribute`) if the specified attribute is accessed (e.g., `$user->created_at`). ## Using Traits `Wildfire` provides traits that are based from the libraries of `Codeigniter 3` such as `Form Validation` and `Pagination Class`. They are used to easily attach the specified functionalities of `Codeigniter 3` to a model. ### `PaginateTrait` The `PaginateTrait` is used to easily create pagination links within the model: ``` php // ciacme/application/models/User.php use Rougin\Wildfire\Traits\PaginateTrait; class User extends \Rougin\Wildfire\Model { use PaginateTrait; // ... }

``` php // ciacme/application/controllers/Welcome.php, (*38)

// Create a pagination links with 10 as the limit and // 100 as the total number of items from the result. $result = $this->user->paginate(10, 100);, (*39)

$data = array('links' => $result[1]);, (*40)

$offset = $result[0];, (*41)

// The offset can now be used for filter results // from the specified table (e.g., "users"). $items = $this->user->get(10, $offset);, (*42)


The `$result[0]` returns the computed offset while `$result[1]` returns the generated pagination links: ``` php // ciacme/application/views/users/index.php

To configure the pagination library, the $pagee property must be defined in the Model:, (*43)

``` php // ciacme/application/models/User.php, (*44)

use Rougin\Wildfire\Traits\PaginateTrait;, (*45)

class User extends \Rougin\Wildfire\Model { use PaginateTrait;, (*46)

// ...

/**
 * Additional configuration to Pagination Class.
 *
 * @link https://codeigniter.com/userguide3/libraries/pagination.html#customizing-the-pagination
 *
 * @var array<string, mixed>
 */
protected $pagee = array(
    'page_query_string' => true,
    'use_page_numbers' => true,
    'query_string_segment' => 'p',
    'reuse_query_string' => true,
);

}, (*47)


> [!NOTE] > Please see the documentation of [Pagination Class](https://codeigniter.com/userguide3/libraries/pagination.html#customizing-the-pagination) to get the list of its available configuration. ### `ValidateTrait` This trait is used to simplify the specifying of validation rules to a model: ``` php // ciacme/application/models/User.php use Rougin\Wildfire\Traits\ValidateTrait; class User extends \Rougin\Wildfire\Model { use ValidateTrait; // ... }

When used, the $rules property of the model must be defined with validation rules that conforms to the Form Validation specification:, (*48)

``` php // ciacme/application/models/User.php, (*49)

use Rougin\Wildfire\Traits\ValidateTrait;, (*50)

class User extends \Rougin\Wildfire\Model { use ValidateTrait;, (*51)

// ...

/**
 * List of validation rules.
 *
 * @link https://codeigniter.com/userguide3/libraries/form_validation.html#setting-rules-using-an-array
 *
 * @var array<string, string>[]
 */
protected $rules = array(
    array('field' => 'name', 'label' => 'Name', 'rules' => 'required'),
    array('field' => 'email', 'label' => 'Email', 'rules' => 'required'),
);

}, (*52)


> [!NOTE] > Kindly check [its documentation](https://codeigniter.com/userguide3/libraries/form_validation.html#setting-rules-using-an-array) for the available rules that can be used to the `$rules` property. To do a form validation, the `validate` method must be called from the model: ``` php // ciacme/application/controllers/Welcome.php /** @var array<string, mixed> */ $input = $this->input->post(null, true); $valid = $this->user->validate($input);

If executed with a view, the validation errors can be automatically be returned to the view using the form_error helper:, (*53)

``` php // ciacme/application/views/users/create.php, (*54)

= form_open('users/create') ?>
= form_error('name') ?>
= form_error('email') ?>

= form_close() ?>, (*55)


### `WritableTrait` The `WritableTrait` is a special trait that enables the model to perform CRUD operations: ``` php // ciacme/application/models/User.php use Rougin\Wildfire\Traits\WritableTrait; class User extends \Rougin\Wildfire\Model { use WritableTrait; // ... }

If added, the model can now perform actions such as create, delete, and update:, (*56)

``` php // ciacme/application/controllers/Welcome.php, (*57)

/** @var array<string, mixed> */ $input = $this->input->post(null, true);, (*58)

// Create the user with the given input $this->user->create($input);, (*59)

// Delete the user based on its ID. $this->user->delete($id);, (*60)

// Update the user details with its input $this->user->update($id, $input);, (*61)


> [!NOTE] > When using this trait, the `CREATED_AT` and `UPDATED_AT` constants will be populated if the `$timestamps` property of a model is enabled. ### `WildfireTrait` Similar to `WritableTrait`, the `WildfireTrait` allows the model to use methods directly from the `Wildfire` class: ``` php // ciacme/application/models/User.php use Rougin\Wildfire\Traits\WildfireTrait; class User extends \Rougin\Wildfire\Model { use WildfireTrait; // ... }

Adding it to a model enables the methods such as find and get methods without specifying the database table:, (*62)

``` php // ciacme/application/controllers/Welcome.php, (*63)

/** @var array<string, mixed> */ $input = $this->input->post(null, true);, (*64)

// Find the user based on the given ID $item = $this->user->find($id);, (*65)

// Return a filtered list of users based on // the specified limit and its given offset $items = $this->user->get($limit, $offset);, (*66)


## Migrating to the `v0.5.0` release The new release for `v0.5.0` will be having a [backward compatibility](https://en.wikipedia.org/wiki/Backward_compatibility) break (BC break). With this, some functionalities from the earlier versions might not be working after upgrading. This was done to increase the maintainability of the project while also adhering to the functionalities for both `Codeigniter 3` and `Eloquent ORM`. Please see the [UPGRADING][link-upgrading] page for the said breaking changes. > [!TIP] > If still using the `v0.4.0` release, kindly click its documentation below: > https://github.com/rougin/credo/blob/v0.4.0/README.md ## Changelog Please see [CHANGELOG][link-changelog] for more information what has changed recently. ## Testing ``` bash $ composer test

Credits

License

The MIT License (MIT). Please see LICENSE for more information., (*67)

The Versions

17/05 2018

dev-master

9999999-dev https://github.com/rougin/wildfire

Query Builder wrapper for the Codeigniter framework.

  Sources   Download

MIT

The Requires

 

The Development Requires

php query codeigniter builder wildfire

07/01 2017

v0.4.0

0.4.0.0 https://github.com/rougin/wildfire

Yet another wrapper for CodeIgniter's Query Builder Class

  Sources   Download

MIT

The Requires

 

The Development Requires

php query codeigniter builder wildfire

12/09 2016

v0.3.1

0.3.1.0 https://github.com/rougin/wildfire

Yet another wrapper for CodeIgniter's Query Builder Class

  Sources   Download

MIT

The Requires

 

The Development Requires

php query codeigniter builder wildfire

05/06 2016

v0.3.0

0.3.0.0 https://github.com/rougin/wildfire

Yet another wrapper for CodeIgniter's Query Builder Class

  Sources   Download

MIT

The Requires

 

The Development Requires

php query codeigniter builder wildfire

13/05 2016

v0.2.1

0.2.1.0 https://github.com/rougin/wildfire

Yet another wrapper for CodeIgniter's Query Builder Class

  Sources   Download

MIT

The Requires

 

The Development Requires

php query codeigniter builder wildfire

24/03 2016

v0.2.0

0.2.0.0 https://github.com/rougin/wildfire

Yet another wrapper for CodeIgniter's Query Builder Class

  Sources   Download

MIT

The Requires

 

The Development Requires

php query codeigniter builder wildfire

06/03 2016

v0.1.0

0.1.0.0 https://github.com/rougin/wildfire

Yet another wrapper for CodeIgniter's Query Builder Class

  Sources   Download

MIT

The Requires

 

The Development Requires

php query codeigniter builder wildfire