Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
97 lines (67 sloc) 2.51 KB

Codeigniter SimpleModel

Simple management and validation models your Codeigniter App.

Introduction

Once you've got the spark set up, you can load it using:

  $this->load->spark('simple_model/X.X.X');

or in autoload.php file:

  $autoload['sparks'] = array('simple_model/X.X.X');

To learn how to configure Spark, visit: http://getsparks.org/get-sparks

Install

  php tools\spark install -vX.X.X simple_model

Usage

Create your model extending the class SimpleModel. Set $table(string) and $fields(array) attributes.

Example model: /sparks/simple_model/X.X.X/models/user_model.php

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class User_model extends SimpleModel {

    function __construct() {
        parent::__construct();
        $this->table = "tb_user";
        $this->fields = array(
            'name' => array(
                'label' => 'Name',
                'rules' => 'required|min_length[5]|max_length[50]'
            ),
            'phone' => array(
                'label' => 'Phone',
                'rules' => 'required',
                'after_send' => 'unmask'
            )
        );
    }

}

Example controller: application/controllers/welcome.php

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Welcome extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('user_model', 'user');
    }

    public function index() {
        try {
            echo '<h1>Simple Model</h1>';

            //Insert..
            $id = $this->user->insert(array('name' => 'Insert Test', 'phone' => '5559-8888'));
            echo "<pre>Insert: $id</pre>";

            //Update..
            $this->user->update($id, array('name' => 'Name Updated'));
            echo "<pre>Update: $id</pre>";

            //Get..
            $result = $this->user->get_where(array('id' => $id));
            $all = $result->result_array();

            echo '<h2>Result:</h2>';
            var_dump($all);

        } catch (Exception $e) {
            var_dump($e);
        }
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
Something went wrong with that request. Please try again.