PHP code example of faqzul / codeigniter-crud-model

1. Go to this page and download the library: Download faqzul/codeigniter-crud-model library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

faqzul / codeigniter-crud-model example snippets


$autoload['packages'] = array();
↓
$autoload['packages'] = array(FCPATH . 'vendor/faqzul/codeigniter-crud-model');

$config['composer_autoload'] = FALSE;
↓
$config['composer_autoload'] = FCPATH . 'vendor/autoload.php';

$this->load->model('crud');

$config['delete_record'] = TRUE;
$config['log_query'] = FALSE;
$config['track_trans'] = FALSE;

$this->crud->initialize($config);

class Welcome extends CI_Controller {

	public function __construct() { parent::__construct(); }

	public function add() {
		$data = array(
			'user_email' => '[email protected]',
			'user_name' => 'FaqZul'
		);
		$a = $this->crud->createData('users', $data, TRUE);
		if ($a['message'] === '') {
			// Success inserting data.
			$profile = array(
				'user_id' => $a['insert_id'],			// it's the same $this->crud->insert_id().
				'link' => 'https://github.com/FaqZul'
			);
			$this->crud->createData('user_profiles', $profile);	// Without callback.
			$id = $this->crud->insert_id();				// Without callback, You can also get insert_id as well.
			redirect("profile?id=$id");
		}
		else {
			// Fail inserting data.
			echo var_dump($a);
		}
	}

	/* Example for insert batch */
	public function insert_batch() {
		$datas = array();
		for ($i = 0; $i < 100; $i++) {
			$data["user_email"] = "[email protected]";
			$data["user_name"] = "FaqZul$i";
			array_push($datas, $data);
		}
		$a = $this->crud->createData('users', $datas);
		/* For get insert_id in every data, please use method insert_ids() */
		echo var_dump($this->crud->insert_ids());
	}

}

$config['insert_id_key'] = 'SequenceName';
$this->crud->initialize($config);

class Welcome extends CI_Controller {

	public function __construct() { parent::__construct(); }

	public function list($page = 0) {
		$a = $this->crud->readData('*', 'users')->result();
		// This method returns the query result as an array of objects, or an empty array on failure. Typically you’ll use this in a foreach loop.
		// Executes: SELECT * FROM users

		$where = array('username' => 'FaqZul');
		$b = $this->crud->readData('*', 'users', $where)->row();
		// This method returns a single result row. If your query has more than one row, it returns only the first row. The result is returned as an object.
		// Executes: SELECT * FROM users WHERE username = 'FaqZul'

		$join = array('user_profiles' => 'users.id = user_profiles.user_id');
		$where = array('username !=' => 'FaqZul');
		$c = $this->crud->readData('*', 'users', $where, $join, '', 'users.id DESC', array(10, $page * 10))->result_array();
		// This method returns the query result as a pure array, or an empty array when no result is produced. Typically you’ll use this in a foreach loop.
		// Executes: SELECT * FROM users JOIN user_profiles ON users.id = user_profiles.user_id WHERE username != 'FaqZul' ORDER BY users.id DESC LIMIT 10
	}

	public function search($q = '') {
		/* You can use more specific LIKE queries. */
		$like = array(array('like' => array('user_name' => $q)), 'or_like' => array('user_email' => $q));
		/* Or You can use it. */
		$like = array('or_like' => array('user_name' => $q, 'user_email' => $q));
		// Executes: WHERE user_name LIKE '%$q%' OR user_email LIKE '%$q%'
		$a = $this->crud->readData('*', 'users', $like)->result_array();
		echo json_encode($a);
	}

	public function user() {
		/* Only show users who are guests or owners. */
		$where = ['or_where' => [['role' => 'guest'], ['role' => 'owner']]];
		$a = $this->crud->readData('*', 'users', $where)->result_array();
		echo json_encode($a);
	}

	// See: https://codeigniter.com/user_guide/database/query_builder.html#query-grouping
	public function search_group() {
		$where = ['a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd'];
		$this->crud->group_set([0 => 'group_start', 1 => 'or_group_start']);
		$this->crud->group_end([2 => 2]);
		echo $this->crud->readDataQuery('*', 'mytable', $where);
		// Print: SELECT * FROM "mytable" WHERE ( "a" = 'a' OR ( "b" = 'b' AND "c" = 'c' ) ) AND "d" = 'd'

		// Search username administrator or superadmin in role admin
		$where = ['role' => 'admin', 'or_where' => [['username' => 'administrator'], ['username' => 'superadmin']]];
		echo $this->crud->group_set([1 => 'group_start'])->group_end([1 => 1])->readDataQuery('*', 'user', $where);
		// Print: SELECT * FROM "user" WHERE "role" = 'admin' AND ( "username" = 'administrator' OR "username" = 'superadmin' )
	}

}

class Welcome extends CI_Controller {

	public function __construct() { parent::__construct(); }

	public function edit($id = 0) {
		$data = array('link' => 'https://github.com/FaqZul/CodeIgniter-CRUD-Model');
		echo ($this->crud->updateData('user_profiles', $data, array('user_id' => $id))) ? 'Success updating data.': $this->crud->error_message();
	}

}

class Welcome extends CI_Controller {

	public function __construct() { parent::__construct(); }

	public function delete($id = 0) {
		if ($this->crud->deleteData('users', array('id' => $id))) {
			// Success deleting data.
			$this->crud->deleteData('user_profiles', array('user_id' => $id));
		}
		else {
			// Fail deleting data.
			echo var_dump($this->crud->error());
		}
	}

}

class Blog_model extends Crud {

	public function __construct() {
		parent::__construct();
		// You can initialize crud in here.
		// $this->initialize($config);
	}

	public function get_entries() {
		$query = $this->readData('*', 'entries');
		return $query->result_array();
	}

	public function insert_entry() {
		$insert = array(
			'title' => $_POST['title'], // please read the below note.
			'content' => $_POST['content'],
			'date' => time()
		);
		$this->createData('entries', $insert);
	}

	public function update_entry() {
		$update = array(
			'title' => $_POST['title'], // please read the below note.
			'content' => $_POST['content'],
			'date' => time()
		);
		$this->updateData('entries', $update, array('id' => $_POST['id']));
	}

}