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

I do not see any advantages. Why should i use this? The code is not good.
Reply
#3

Hello!

What are the advantages of using this over the builtin query builder?
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Reply
#4

Hello @Paradinight and @albertleao,

I have now updated the documentation on how to use it and what can it do. Thanks.
Reply
#5

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

$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(); 

You code is too complicated.

I am using
PHP Code:
$this->load->model('post_model');
$posts $this->post_model->where('deleted'0)->find_all(); 

or
PHP Code:
$this->load->model('post_model');
$posts $this->post_model->query_result('SELECT * FROM post where deleted = 1'); 

You addin is too fat. Other crud addins a better.
Reply
#6

(This post was last modified: 06-09-2016, 08:39 AM by albertleao.)

Hey @rougin,

I'm still wondering what benefits there are to using this. I commend your work, but why would I use this over CI's query builder or what I use currently (Eloquent).

My current code works as simple as :

PHP Code:
//User object
$user = \Models\User::where('id''='1)->first();

//User posts
$posts $user->posts;

//Convert posts to array
$array_of_posts $posts->toArray(); 
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB