Skip to content

Instantly share code, notes, and snippets.

@MauMaGau
Last active December 18, 2015 02:58
Show Gist options
  • Save MauMaGau/5714431 to your computer and use it in GitHub Desktop.
Save MauMaGau/5714431 to your computer and use it in GitHub Desktop.
Sublime CI model snippet
<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('$1_id', \$$1_id)
->get();
return \$query->row();
}
public function get_by(\$columns_values, \$limit=FALSE, \$offset=0, \$order=FALSE, \$count=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){
// Insert
\$$1_id = uniqid();
\$this->db
->set('$1_id', \$$1_id)
->insert('$1');
}else{
// Update
\$this->db
->where('$1_id', \$$1_id)
->update('$1');
}
return \$$1_id;
}
public function delete(\$$1_id)
{
if(empty(\$$1_id)){
return FALSE;
}
\$this->db
->where('$1_id', \$$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