Git Product home page Git Product logo

codeigniter-my_model's Introduction

CodeIgniter-MY_Model

Replaces CodeIgniter's default model and adds CRUD helper functions along with validation against a schema.

Example

The following is an example of the schema you need to write for each table/model in your application. Since this custom class provides the basic CRUD functions such as create() you do not need to define these for every model and eliminates repeated code. The CRUD functions are all validated against the schema you write so that if you inadvertly forget to pass a value for a required (not null) column, a catachable exception will be thrown with a clear message why the query was rejected, all before the query is even sent to the database. Other validations include data type checking, protection against string truncation, conversion from empty string to NULL (when applicable).

class User_model extends MY_Model 
{	
	function __construct()
	{
		parent::MY_Model();
		
		$this->schema = array( //       < DATATYPE  | LENGTH | FLAGS  | DEFAULT >
			'id'				=> array(INT		, NULL	, PK+AI+UN,	NULL),
			'username'			=> array(VARCHAR	, 45	, NN,		NULL),
			'password'			=> array(CHAR		, 40	, NN,		NULL),
			'salt'				=> array(CHAR		, 40	, NN,		NULL),
			'email'				=> array(VARCHAR	, NULL	, NN,		NULL),
			'realname'			=> array(VARCHAR	, NULL	, NULL,		NULL),
			'date_joined'		=> array(DATETIME	, NULL	, NN,		$this->now()),
			'last_login'		=> array(DATETIME	, NULL	, NN,		$this->now()),
			'active'			=> array(TINYINT	, 1		, NN,		1)
		);
		
		$this->before_create[] = '_prepare_password';
		$this->before_update[] = '_prepare_password';
	}
}

Requirements

  • CodeIgniter 2.0
  • Schema definitions for each model

codeigniter-my_model's People

Contributors

istvanp avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.