Created
August 3, 2014 19:36
-
-
Save patpohler/e9267edc5f65af21afd9 to your computer and use it in GitHub Desktop.
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
<?php if(!defined('EXT')) exit("Invalid file request"); | |
/** | |
* Post Model Class | |
* | |
* @package ci_model_example | |
* @author Patrick Pohler [email protected] | |
* @copyright Copyright (c) 2014, Patrick Pohler | |
* @link http://www.anecka.com/rets_press | |
* @license MIT | |
*/ | |
class Post extends CI_Model { | |
var $id; | |
var $site_id; | |
var $title = ''; | |
var $name = ''; | |
var $date = null; | |
var $author_secret = ''; | |
function __construct() { | |
ee()->load->library('encrypt'); | |
parent::__construct(); | |
} | |
public function get($id) { | |
$query = ee()->db->get_where($this->_get_table(), array('id' => $id)); | |
if($query->num_rows() == 1) $this->_set_model_for_return($query->row()); | |
} | |
public function get_by_site_id($site_id) { | |
$query = ee()->db->get_where($this->_get_table(), array('site_id' => $site_id)); | |
if($query->num_rows() == 1) $this->_set_model_for_return($query->row()); | |
} | |
public function insert($data) { | |
$this->_set_model_for_save($data); | |
ee()->db->insert($this->_get_table(), $this); | |
$this->id = ee()->db->insert_id(); | |
} | |
public function update($data, $id) { | |
$this->_set_model_for_save($data); | |
$this->id = $id; | |
ee()->db->update($this->_get_table(), $this, array('id' => $id)); | |
} | |
private function _set_model_for_save($data) { | |
$this->site_id = $data['site_id']; | |
$this->title = $data['title']; | |
$this->name = $data['name']; | |
$this->date = $data['date']; | |
$this->author_secret = ee()->encrypt->encode($data['author_secret']); | |
} | |
private function _set_model_for_return($row) { | |
$this->id = $row->id; | |
$this->site_id = $row->site_id; | |
$this->title = $row->title; | |
$this->name = $row->name; | |
$this->date = $row->date; | |
$this->author_secret = ee()->encrypt->decode($row->author_secret); | |
} | |
private function _get_table(){ | |
return "posts"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment