CRUD Library for Ext.CodeIgniter: An Example

Share

Our Model: Site_model

<?php

class Site_model extends Model {

	function get($limit = 10, $offset = 0)
	{
		// Set empty storage
		$output['data'] = array();

		// Table header
		$output['header'] = array ('ID', 'Title', 'Content', 'Delete');

		// Query database and generate record array to used in Table Library
		$this->db->from('data');
		$this->db->limit($limit, $offset);
		$record = $this->db->get();

		foreach ($record->result() as $row)
		{
			array_push($output['data'], array(
				$row->id,
				// set an edit link
				anchor('site/modify/' . $row->id, $row->title),
				$row->content,
				// set delete link
				anchor('site/delete/' . $row->id, 'Delete this record')
			));
		}

		// Get total rows for pagination purpose.
		$output['total_rows'] = $this->db->from('data')->count_all_results();

		return $output;
	}

	function update($data, $response)
	{
		// with Set data in ActiveRecord, you save time by doing insert and update at the same time
		$this->db->set('title', $data['title']);
		$this->db->set('content', $data['content']);

		// only new record have id = 0
		if ($data['id'] > 0) :
			$this->db->where('id', $data['id']);
			$this->db->update('data');
		else :
			$this->db->insert('data');
		endif;

		// since by right it should both return success
		$response['success'] = TRUE;

		// redirect to retrieve option
		$response['redirect'] = site_url('site/index');

		return $response;
	}

	function remove($id, $response)
	{
		// chaining in ActiveRecord can save you time
		$check_record = $this->db->from('data')->where('id', $id)->limit(1)->get();

		if ($check_record->num_rows() > 0)
		{
			$this->db->delete('data', array('id' => $id));

			// since by right it should both return success
			$response['success'] = TRUE;

			// redirect to retrieve option
			$response['redirect'] = site_url('site/index');
		}
		else
		{
			$response['error'] = 'Data does not exist';
		}

		return $response;
	}

	function get_fields()
	{
		/* setup fields related to this model, id must be unique
		 * work as a linkage to the database field
		 */
		return array (
			array (
				'id' => 'id',
				'type' => 'hidden'
			),
			array (
				'id' => 'title',
				'name' => 'Title',
				'type' => 'text',
				'xss' => TRUE,
				'rule' => 'trim|required'
			),
			array (
				'id' => 'content',
				'name' => 'Content',
				'type' => 'textarea',
				'rule' => 'xss_clean|trim'
			)
		);
	}

	function get_one($id = 0)
	{
		// default value array key should be the same with 'id's in fields
		$data = array (
			'id' => 0,
			'title' => 'A title',
			'content' => ''
		);

		$record = $this->db->from('data')->where('id', $id)->get();

		// key exist in table, fetch it's value
		if ($record->num_rows() > 0)
		{
			$row = $record->row();

			$data['id'] = $row->id;
			$data['title'] = $row->title;
			$data['content'] = $row->content;
		}

		return $data;
	}
}
/* End of file site_model.php */
/* Location: ./system/application/models/site_model.php */

Comparing Model with Controller and View you would definitely guess it right. Most of the work has been moved to the Model in order to use CRUD Library. This may not be the right method to use Model as an Object in OOP (I know some people would be bashing on me), however the code is more organized and in most cases if you need to change the structure of data table e.g: adding a new column all you need to do is modify the Model.

Pages: 1 2 3 4 5

Leave your comment