PHPackages                             rougin/wildfire - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. rougin/wildfire

ActiveLibrary[Database &amp; ORM](/categories/database)

rougin/wildfire
===============

Eloquent-like ORM for Codeigniter 3.

v0.6.1(3w ago)181.4k1MITPHPPHP &gt;=5.4.0CI passing

Since Mar 5Pushed 3w ago3 watchersCompare

[ Source](https://github.com/rougin/wildfire)[ Packagist](https://packagist.org/packages/rougin/wildfire)[ Docs](https://roug.in/wildfire/)[ RSS](/packages/rougin-wildfire/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (9)Dependencies (8)Versions (10)Used By (0)

Wildfire
========

[](#wildfire)

[![Latest Version on Packagist](https://camo.githubusercontent.com/7779436099ec9e09f369f4f275ab23e1764b3642eefdc6242ed5c392005ddbf1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f726f7567696e2f77696c64666972652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/rougin/wildfire)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/rougin/wildfire/blob/master/LICENSE.md)[![Build Status](https://camo.githubusercontent.com/12aa21b3e68a779a96d513104cc2796e10950eab31768657746f93b7ce6dfbb1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f726f7567696e2f77696c64666972652f6275696c642e796d6c3f7374796c653d666c61742d737175617265)](https://github.com/rougin/wildfire/actions)[![Coverage Status](https://camo.githubusercontent.com/060814e88e3b685585b192aff1a7d633909364528cd4dd1fb3399db5eeeffb7a/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f726f7567696e2f77696c64666972653f7374796c653d666c61742d737175617265)](https://app.codecov.io/gh/rougin/wildfire)[![Total Downloads](https://camo.githubusercontent.com/b2c3fe19d049a26a1f6ffae1edb529f1e9b4a54cf2590ac9db7312df52d9c8ac/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726f7567696e2f77696c64666972652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/rougin/wildfire)

Wildfire is a simple wrapper for the [Query Builder Class](https://codeigniter.com/user_guide/database/query_builder.html) based on [Codeigniter 3](https://codeigniter.com/userguide3/). It is inspired from the [Eloquent ORM](https://laravel.com/docs/5.6/eloquent) of Laravel.

Installation
------------

[](#installation)

Install `Wildfire` via [Composer](https://getcomposer.org/):

```
$ composer require rougin/wildfire
```

Basic Usage
-----------

[](#basic-usage)

### Prerequisites

[](#prerequisites)

Create a sample database table to be used (e.g., `users`):

```
-- 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`:

```
// ciacme/application/config/config.php

/*
|--------------------------------------------------------------------------
| 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';
```

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`:

```
// ciacme/application/models/User.php

use Rougin\Wildfire\Model;

class User extends Model
{
}
```

```
// ciacme/application/controllers/Welcome.php

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

// 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');

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

### Using `Wildfire`

[](#using-wildfire)

### With `CI_DB_query_builder`

[](#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:

```
// 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`

[](#with-ci_db_result)

Aside from using methods of `Wildfire`, raw SQL queries can also be converted to its `Model` counterpart:

```
// ciacme/application/controllers/Welcome.php

use Rougin\Wildfire\Wildfire;

$query = 'SELECT p.* FROM post p';

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

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

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

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

Properties of `Model` class
---------------------------

[](#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

[](#casting-attributes)

Updating the `$casts` property allows the model to cast native types to the specified attributes:

```
// 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**

```
{
    "id": 1,
    "name": "Rougin",
    "age": "20",
    "gender": "male",
    "accepted": "0",
}
```

**With native casts**

```
{
    "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.

### Hiding attributes

[](#hiding-attributes)

To hide attributes for serialization, the `$hidden` property can be used:

```
// ciacme/application/models/User.php

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

**Without hidden attributes**

```
{
    "id": 1,
    "name": "Rougin",
    "age": "20",
    "gender": "male",
    "accepted": "0",
}
```

**With hidden attributes**

```
{
    "id": 1,
    "name": "Rougin",
    "age": "20",
    "accepted": "0",
}
```

In this example, the `gender` field was not included in the result.

### Visible attributes

[](#visible-attributes)

Opposite of the `$hidden` property, the `$visible` property specifies the fields to be visible in the result:

```
// 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**

```
{
    "id": 1,
    "name": "Rougin",
    "age": "20",
    "gender": "male",
    "accepted": "0",
}
```

**With visible attributes**

```
{
    "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.

### Using timestamps

[](#using-timestamps)

Similar to `Eloquent ORM`, `Wildfire` enables the usage of timestamps by default:

```
// ciacme/application/models/User.php

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

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`):

```
// 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`.

### Customizing attributes

[](#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`:

```
// ciacme/application/models/User.php

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']));
    }
}
```

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
------------

[](#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`

[](#paginatetrait)

The `PaginateTrait` is used to easily create pagination links within the model:

```
// ciacme/application/models/User.php

use Rougin\Wildfire\Traits\PaginateTrait;

class User extends \Rougin\Wildfire\Model
{
    use PaginateTrait;

    // ...
}
```

```
// ciacme/application/controllers/Welcome.php

// 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);

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

$offset = $result[0];

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

The `$result[0]` returns the computed offset while `$result[1]` returns the generated pagination links:

```
// ciacme/application/views/users/index.php

```

To configure the pagination library, the `$pagee` property must be defined in the `Model`:

```
// ciacme/application/models/User.php

use Rougin\Wildfire\Traits\PaginateTrait;

class User extends \Rougin\Wildfire\Model
{
    use PaginateTrait;

    // ...

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

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`

[](#validatetrait)

This trait is used to simplify the specifying of validation rules to a model:

```
// 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:

```
// ciacme/application/models/User.php

use Rougin\Wildfire\Traits\ValidateTrait;

class User extends \Rougin\Wildfire\Model
{
    use ValidateTrait;

    // ...

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

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:

```
// ciacme/application/controllers/Welcome.php

/** @var array */
$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:

```
// ciacme/application/views/users/create.php
