Welcome Guest, Not a member yet? Register   Sign In
Wildfire - Yet another wrapper for Query Builder
#1

(This post was last modified: 06-05-2016, 12:22 AM by rougin.)

Hello! I've created a library named Wildfire, yet another wrapper for CodeIgniter's Query Builder class. It can generate CI_Model objects that is based from database tables with ease. https://github.com/rougin/wildfire

Installation

You can install it via Composer:

Code:
$ composer require rougin/wildfire

NOTE: composer_autoload must be enabled in the application/config/config.php.

Basic Usage

Tables (in SQLite)

Code:
CREATE TABLE "user" (
   "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
   "name" TEXT NOT NULL,
   "age" INTEGER NOT NULL,
   "gender" TEXT NOT NULL
);

models/User.php

PHP Code:
class User extends CI_Model {} 

Code:
CREATE TABLE post (
   id INTEGER PRIMARY KEY,
   subject TEXT NOT NULL,
   message TEXT NOT NULL,
   user_id INTEGER,
   description TEXT NULL,
   FOREIGN KEY(user_id) REFERENCES user(id)
);

models/Post.php

PHP Code:
class Post extends CI_Model {} 

Using Query Builder

PHP Code:
$this->load->model('post');
$this->load->model('user');

// Build your queries here
$this->db->like('subject''Foo Bar''both');

// Instantiate Wildfire with the CI_DB class
$wildfire = new Rougin\Wildfire\Wildfire($this->db);

// Returns an array of Post objects with a User object per Post object
$posts $wildfire->get('post')->result(); 

Using raw SQL query

PHP Code:
$this->load->model('post');
$this->load->model('user');

$query $this->db->query('SELECT * FROM post');

// Instantiate Wildfire with the database class and the query
$wildfire = new Rougin\Wildfire\Wildfire($this->db$query);

// Returns an array of Post objects with a User object per Post object
$posts $wildfire->result(); 

Methods

$wildfire->find($table, $delimiters = [])

PHP Code:
// Returns a post with an ID of 1.
$posts $wildfire->find('post'1);

// Returns a post from the provided delimiters.
$posts $wildfire->find('post', [ 'name' => 'Foo Bar' ]); 

$wildfire->get($table = '')->as_dropdown($description = 'description')

PHP Code:
// Returns a list of posts that can be used in form_dropdown().
// $description means what column will be used to display.
$posts $wildfire->get('post')->as_dropdown('subject'); 

$wildfire->set_database($this->db)

PHP Code:
// Sets as the current database
$wildfire->set_database($this->db); 

$wildfire->set_query()

PHP Code:
// Sets as the current query
$wildfire->set_query('SELECT * FROM posts'); 

Model Conventions

PHP Code:
class Post extends CI_Model {

 
   /**
     * The table associated with the model.
     * If not set, it will get the name of the database table.
     *  
     * @var string
     */
 
   public $table 'post';

    
/**
     * Columns that will be displayed.
     * If not set, it will get the columns from the database table.
     *  
     * @var array
     */
    
public $columns = array(
        
'id',
        
'subject',
        
'message',
        
'user_id',
        
'description',
    );


Reply


Messages In This Thread
Wildfire - Yet another wrapper for Query Builder - by rougin - 05-31-2016, 12:00 PM



Theme © iAndrew 2016 - Forum software by © MyBB