Created
April 18, 2013 10:35
-
-
Save MauMaGau/5411772 to your computer and use it in GitHub Desktop.
CI MODEL SNIPPET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<snippet> | |
<content><![CDATA[ | |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
class $1_model extends ${2:CI}_Model { | |
public function get_by_id(\$$1_id) | |
{ | |
\$query = \$this->db | |
->from('$1') | |
->where('uid', \$$1_id) | |
->get(); | |
return \$query->row(); | |
} | |
public function get_by(\$columns_values, \$limit=FALSE, \$offset=0, \$order=FALSE) | |
{ | |
\$this->db | |
->from('$1'); | |
foreach(\$columns_values as \$column=>\$value){ | |
if(is_array(\$value)){ | |
if(empty(\$value)){ | |
return array(); | |
} | |
\$this->db | |
->where_in(\$column, \$value); | |
}else{ | |
\$this->db | |
->where(\$column, \$value); | |
} | |
} | |
if(\$limit!==FALSE){ | |
\$this->db | |
->limit(\$limit, \$offset); | |
} | |
if(\$order!==FALSE){ | |
\$this->db | |
->order_by(\$order); | |
} | |
if(\$count===TRUE){ | |
return \$this->db | |
->count_all_results(); | |
} | |
\$query = \$this->db | |
->get(); | |
return \$query->result(); | |
} | |
public function save(\$$1_id=FALSE, \$data) | |
{ | |
\$this->db | |
->set(\$data); | |
if(\$$1_id===FALSE){ | |
// Update | |
\$$1_id = uniqid(); | |
\$this->db | |
->set('uid', \$$1_id) | |
->insert('$1'); | |
}else{ | |
// Insert | |
\$this->db | |
->where('uid', \$$1_id) | |
->update('$1'); | |
} | |
return \$$1_id; | |
} | |
public function delete(\$$1_id) | |
{ | |
if(empty(\$$1_id)){ | |
return FALSE; | |
} | |
\$this->db | |
->where('uid', \$$1_id) | |
->limit(1) | |
->delete('$1'); | |
} | |
} | |
/* End of file ${TM_FILENAME:${1/(.+)/\l$1.php/}} */ | |
/* Location: ./${TM_FILEPATH/.+((?:application).+)/$1/:application/models/${1/(.+)/\l$1.php/}} */ | |
]]></content> | |
<tabTrigger>model</tabTrigger> | |
<scope>source.php, text.html.basic, text.plain</scope> | |
<description>CI - Base Model</description> | |
</snippet> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment