shiva666

undefined method CI_Model::get()

Dec 23rd, 2013
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.31 KB | None | 0 0
  1. Pages controller
  2. [code]
  3. <?php if(!defined('BASEPATH')) exit ('No direct script access allowed');
  4.  
  5. class Pages extends MX_Controller{
  6.  
  7.     public function __construct() {
  8.         parent::__construct();
  9.         $this->load->model('pages_model');
  10.         $this->load->library('form_validation');
  11.     }
  12.  
  13.     function manage(){
  14.         $data['pages'] = $this->pages_model->get();
  15.    
  16.         $data['view_file'] = "manage";
  17.         $this->load->module('templates');
  18.         $this->templates->admin($data);
  19.     }
  20.    
  21.     public function edit($id = NULL){
  22.         if($id){
  23.             $data['page'] = $this->pages_model->get($id);
  24.             count($data['page']) || $data['errors'][] = 'User could not be found';
  25.         } else {
  26.             $data['page'] = $this->pages_model->get_new();
  27.         }
  28.        
  29.         //pages for dropdown
  30.         $data['pages_no_parents'] = $this->pages_model->get_no_parents();
  31.         dump($data['pages_no_parents']);
  32.        
  33.         //$id == NULL || $data['page'] = $this->pages_model->get($id);
  34.         $rules = $this->pages_model->rules;
  35.         $this->form_validation->set_rules($rules);
  36.         if($this->form_validation->run() == TRUE){
  37.             $data = $this->pages_model->array_from_post(array('title', 'slug', 'body'));
  38.             $this->pages_model->save($data, $id);
  39.             redirect('pages/manage');
  40.         }
  41.  
  42.         $data['view_file'] = "edit";
  43.         $this->load->module('templates');
  44.         $this->templates->admin($data);
  45.     }
  46.  
  47.     public function _unique_slug($str){
  48.         $id = $this->uri->segment(3);
  49.         $this->db->where('slug', $this->input->post('slug'));
  50.         !$id || $this->db->where('id !=', $id);
  51.         $page = $this->pages_model->get();
  52.        
  53.         if(count($page)){
  54.             $this->form_validation->set_message('_unique_slug', '%s should be unique');
  55.             return FALSE;
  56.         }
  57.        
  58.         return TRUE;
  59.     }
  60.    
  61.     public function delete($id){
  62.         $this->pages_model->delete($id);
  63.         redirect('pages/manage');
  64.     }
  65.  
  66.     function get($order_by){
  67.         $this->load->model('pages_model');
  68.         $query = $this->pages_model->get($order_by);
  69.         return $query;
  70.     }
  71.  
  72.     function get_with_limit($limit, $offset, $order_by) {
  73.         $this->load->model('pages_model');
  74.         $query = $this->pages_model->get_with_limit($limit, $offset, $order_by);
  75.         return $query;
  76.     }
  77.  
  78.     function get_where($id){
  79.         $this->load->model('pages_model');
  80.         $query = $this->pages_model->get_where($id);
  81.         return $query;
  82.     }
  83.  
  84.     function get_where_custom($col, $value) {
  85.         $this->load->model('pages_model');
  86.         $query = $this->pages_model->get_where_custom($col, $value);
  87.         return $query;
  88.     }
  89.  
  90.     function _insert($data){
  91.         $this->load->model('pages_model');
  92.         $this->pages_model->_insert($data);
  93.     }
  94.  
  95.     function _update($id, $data){
  96.         $this->load->model('pages_model');
  97.         $this->pages_model->_update($id, $data);
  98.     }
  99.  
  100.     function _delete($id){
  101.         $this->load->model('pages_model');
  102.         $this->pages_model->_delete($id);
  103.     }
  104.  
  105.     function count_where($column, $value) {
  106.         $this->load->model('pages_model');
  107.         $count = $this->pages_model->count_where($column, $value);
  108.         return $count;
  109.     }
  110.  
  111.     function get_max() {
  112.         $this->load->model('pages_model');
  113.         $max_id = $this->pages_model->get_max();
  114.         return $max_id;
  115.     }
  116.  
  117.     function _custom_query($mysql_query) {
  118.         $this->load->model('pages_model');
  119.         $query = $this->pages_model->_custom_query($mysql_query);
  120.         return $query;
  121.     }
  122. }
  123. [/code]
  124.  
  125. and this is my pages_model
  126.  
  127. [code]
  128. <?php if(!defined('BASEPATH')) exit ('No direct script access allowed');
  129.  
  130. class Pages_model extends CI_Model {
  131.    
  132.     protected $_table_name = "pages";
  133.     protected $_primary_key = "id";
  134.     protected $_primary_filter = "intval";
  135.     protected $_order_by = "order";
  136.     public $rules = array(
  137.         'parent_id' => array(
  138.             'field' => 'parent_id',
  139.             'label' => 'Parnet',
  140.             'rules' => 'trim|intval'
  141.         ),
  142.        
  143.         'slug' => array(
  144.             'field' => 'slug',
  145.             'label' => 'Slug',
  146.             'rules' => 'trim|required|max_length[100]|url_title|callback__unique_slug|xss_clean'
  147.         ),
  148.        
  149.         'order' => array(
  150.             'field' => 'order',
  151.             'label' => 'Order',
  152.             'rules' => 'trim|is_natural|xss_clean'
  153.         ),
  154.        
  155.         'body' => array(
  156.             'field' => 'body',
  157.             'label' => 'Body',
  158.             'rules' => 'trim|required'
  159.         ),
  160.        
  161.     );
  162.    
  163.     protected $_timestamps = "FALSE";
  164.  
  165.     public function __construct() {
  166.         parent::__construct();
  167.     }
  168.  
  169.  
  170. public function get($id = NULL, $single = FALSE){
  171.     if($id != NULL){
  172.         $filter = $this->_primary_filter;
  173.         $id = $filter($id);
  174.         $this->db->where($this->_primary_key, $id);
  175.         $method = 'row';
  176.     } elseif($single == TRUE) {
  177.         $method = 'row';
  178.     } else {
  179.         $method = 'result';
  180.     }
  181.  
  182.     return $this->db->get($this->_table_name)->$method();
  183. }
  184.  
  185. public function get_new(){
  186.     $page = new stdClass();
  187.     $page->title = '';
  188.     $page->slug = '';
  189.     $page->body = '';
  190.     $page->parent_id = 0;
  191.     return $page;
  192. }
  193.  
  194. public function get_by($where, $single = FALSE){
  195.     $this->db->where($where);
  196.     return $this->get(NULL, $single);
  197. }
  198.  
  199. public function save($data, $id = NULL){
  200.     //Insert
  201.     if($id === NULL){
  202.         !isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
  203.         $this->db->set($data);
  204.         $this->db->insert($this->_table_name);
  205.         $id = $this->db->insert_id();
  206.     } else {
  207.         //Update
  208.         $filter = $this->_primary_filter;
  209.         $id = $filter($id);
  210.         $this->db->set($data);
  211.         $this->db->where($this->_primary_key, $id);
  212.         $this->db->update($this->_table_name);
  213.     }
  214.     return $id;
  215. }
  216.  
  217. public function delete($id){
  218.     $filter = $this->_primary_filter;
  219.     $id = $filter($id);
  220.    
  221.     if(!$id){
  222.         return FALSE;
  223.     }
  224.    
  225.     $this->db->where($this->_primary_key, $id);
  226.     $this->db->limit(1);
  227.     $this->db->delete($this->_table_name);
  228. }
  229.  
  230. public function hash($string){
  231.     return hash('sha512', $string . config_item('encryption_key'));
  232. }
  233.  
  234. public function array_from_post($fields){
  235.     $data = array();
  236.     foreach($fields as $field){
  237.         $data[$field] = $this->input->post($field);
  238.     }
  239.    
  240.     return $data;
  241. }
  242.  
  243. public function get_no_parents(){
  244.     //Fetch pages without  parents
  245.     $this->db->select('id', 'title');
  246.     $this->db->where('parent_id', 0);
  247.     //$pages = parent::get();
  248.     $pages = $this->get();
  249.     //echo $this->db->last_query();
  250.    
  251.     //Return key => value pair array
  252.     $array = array(0 => 'No parent');
  253.     if (count($pages)){
  254.         foreach($pages as $page){
  255.             $array[$page->id] = $page->title;
  256.         }
  257.     }
  258. }
  259.  
  260. function get_where($id){
  261. $table = $this->get_table();
  262. $this->db->where('id', $id);
  263. $query=$this->db->get($this->_table_name);
  264. return $query;
  265. }
  266.  
  267. function _insert($data){
  268. $table = $this->get_table();
  269. $this->db->insert($this->_table_name, $data);
  270. }
  271.  
  272. }
  273. [/code]
Advertisement
Add Comment
Please, Sign In to add comment