jamierumbelow / codeigniter-base-model

⛔️DEPRECATED CodeIgniter base CRUD model to remove repetition and increase productivity
MIT License
1.05k stars 479 forks source link

Pagination with MY_Model #133

Open mistbow opened 11 years ago

mistbow commented 11 years ago

Hi all; Now How can I get per page of articles using MY_Model?Does MY_Model support pagination right now?

thanks!

jamierumbelow commented 11 years ago

There's nothing special about using pagination with MY_Model, you just need to use the normal CodeIgniter class:

class SomeController extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->load->library('pagination');
        $this->load->model('some_model');
    }

    public function index()
    {
        $config['base_url'] = $this->uri->uri_string();
        $config['total_rows'] = $this->some_model->count_all();
        $config['per_page'] = 20;

        $this->pagination->initialize($config); 

        $this->data['objects'] = $this->some_model->limit($config['per_page'], $this->uri->segment(3));
        $this->data['links'] = $this->pagination->create_links();

        $this->load->view('some_controller/index', $this->data);
    }
}
sgsrra commented 10 years ago

I tried to use this

$this->manufacturers_model->limit($10, 0);

But It does not work.

MyModel returns

object(Manufacturers_model)#50 (24) {
  ["return_type":protected]=>
  string(5) "array"
  ["primary_key"]=>
  string(2) "id"
  ["_table":protected]=>
  string(13) "manufacturers"
  ["_database"]=>
  object(CI_DB_mysql_driver)#13 (73) {
    ["dbdriver"]=>
    string(5) "mysql"
    ["_escape_char"]=>
    string(1) "`"
    ["_like_escape_str"]=>
    string(0) ""
    ["_like_escape_chr"]=>
    string(0) ""
    ["delete_hack"]=>
    bool(true)
    ["_count_string"]=>
    string(19) "SELECT COUNT(*) AS "
    ["_random_keyword"]=>
    string(7) " RAND()"
    ["use_set_names"]=>
    bool(false)
    ["ar_select"]=>
    array(0) {
    }
    ["ar_distinct"]=>
    bool(false)
    ["ar_from"]=>
    array(0) {
    }
    ["ar_join"]=>
    array(0) {
    }
    ["ar_where"]=>
    array(0) {
    }
    ["ar_like"]=>
    array(0) {
    }
    ["ar_groupby"]=>
    array(0) {
    }
    ["ar_having"]=>
    array(0) {
    }
    ["ar_keys"]=>
    array(0) {
    }
    ["ar_limit"]=>
    int(10)
    ["ar_offset"]=>
    bool(false)
    ["ar_order"]=>
    bool(false)
    ["ar_orderby"]=>
    array(0) {
    }
    ["ar_set"]=>
    array(0) {
    }
    ["ar_wherein"]=>
    array(0) {
    }
    ["ar_aliased_tables"]=>
    array(0) {
    }
    ["ar_store_array"]=>
    array(0) {
    }
    ["ar_caching"]=>
    bool(false)
    ["ar_cache_exists"]=>
    array(0) {
    }
    ["ar_cache_select"]=>
    array(0) {
    }
    ["ar_cache_from"]=>
    array(0) {
    }
    ["ar_cache_join"]=>
    array(0) {
    }
    ["ar_cache_where"]=>
    array(0) {
    }
    ["ar_cache_like"]=>
    array(0) {
    }
    ["ar_cache_groupby"]=>
    array(0) {
    }
    ["ar_cache_having"]=>
    array(0) {
    }
    ["ar_cache_orderby"]=>
    array(0) {
    }
    ["ar_cache_set"]=>
    array(0) {
    }
    ["ar_no_escape"]=>
    array(0) {
    }
    ["ar_cache_no_escape"]=>
    array(0) {
    }
    ["username"]=>
    string(4) "root"
    ["password"]=>
    string(10) "mgdrgd7411"
    ["hostname"]=>
    string(9) "localhost"
    ["database"]=>
    string(13) "sonyacart_com"
    ["dbprefix"]=>
    string(3) "si_"
    ["char_set"]=>
    string(4) "utf8"
    ["dbcollat"]=>
    string(15) "utf8_unicode_ci"
    ["autoinit"]=>
    bool(true)
    ["swap_pre"]=>
    string(0) ""
    ["port"]=>
    string(0) ""
    ["pconnect"]=>
    bool(true)
    ["conn_id"]=>
    resource(43) of type (mysql link persistent)
    ["result_id"]=>
    resource(90) of type (mysql result)
    ["db_debug"]=>
    bool(true)
    ["benchmark"]=>
    float(0.00500774383545)
    ["query_count"]=>
    int(21)
    ["bind_marker"]=>
    string(1) "?"
    ["save_queries"]=>
    bool(true)
    ["queries"]=>
    array(21) {
      [0]=>
      string(29) "SELECT *
FROM (`si_settings`)"
      [1]=>
      string(125) "SELECT *
FROM (`si_bk_modules`)
WHERE `parent_id` =  0
AND `active` =  1
AND `module_menu` =  'main_menu'
ORDER BY `position`"
      [2]=>
      string(86) "SELECT `name`
FROM (`si_bk_modules_lang`)
WHERE `id_module` =  '14'
AND `lang_id` =  1"
      [3]=>
      string(77) "SELECT COUNT(*) AS `numrows`
FROM (`si_bk_modules`)
WHERE `parent_id` =  '14'"
      [4]=>
      string(86) "SELECT `name`
FROM (`si_bk_modules_lang`)
WHERE `id_module` =  '10'
AND `lang_id` =  1"
      [5]=>
      string(77) "SELECT COUNT(*) AS `numrows`
FROM (`si_bk_modules`)
WHERE `parent_id` =  '10'"
      [6]=>
      string(128) "SELECT *
FROM (`si_bk_modules`)
WHERE `parent_id` =  '10'
AND `active` =  1
AND `module_menu` =  'main_menu'
ORDER BY `position`"
      [7]=>
      string(86) "SELECT `name`
FROM (`si_bk_modules_lang`)
WHERE `id_module` =  '11'
AND `lang_id` =  1"
      [8]=>
      string(77) "SELECT COUNT(*) AS `numrows`
FROM (`si_bk_modules`)
WHERE `parent_id` =  '11'"
      [9]=>
      string(86) "SELECT `name`
FROM (`si_bk_modules_lang`)
WHERE `id_module` =  '18'
AND `lang_id` =  1"
      [10]=>
      string(77) "SELECT COUNT(*) AS `numrows`
FROM (`si_bk_modules`)
WHERE `parent_id` =  '18'"
      [11]=>
      string(86) "SELECT `name`
FROM (`si_bk_modules_lang`)
WHERE `id_module` =  '12'
AND `lang_id` =  1"
      [12]=>
      string(77) "SELECT COUNT(*) AS `numrows`
FROM (`si_bk_modules`)
WHERE `parent_id` =  '12'"
      [13]=>
      string(128) "SELECT *
FROM (`si_bk_modules`)
WHERE `parent_id` =  '12'
AND `active` =  1
AND `module_menu` =  'main_menu'
ORDER BY `position`"
      [14]=>
      string(86) "SELECT `name`
FROM (`si_bk_modules_lang`)
WHERE `id_module` =  '13'
AND `lang_id` =  1"
      [15]=>
      string(77) "SELECT COUNT(*) AS `numrows`
FROM (`si_bk_modules`)
WHERE `parent_id` =  '13'"
      [16]=>
      string(86) "SELECT `name`
FROM (`si_bk_modules_lang`)
WHERE `id_module` =  '20'
AND `lang_id` =  1"
      [17]=>
      string(77) "SELECT COUNT(*) AS `numrows`
FROM (`si_bk_modules`)
WHERE `parent_id` =  '20'"
      [18]=>
      string(86) "SELECT `name`
FROM (`si_bk_modules_lang`)
WHERE `id_module` =  '21'
AND `lang_id` =  1"
      [19]=>
      string(77) "SELECT COUNT(*) AS `numrows`
FROM (`si_bk_modules`)
WHERE `parent_id` =  '21'"
      [20]=>
      string(226) "SELECT *
FROM (`si_bk_modules`)
JOIN `si_bk_modules_lang` ON `si_bk_modules`.`id` = `si_bk_modules_lang`.`id_module`
WHERE `si_bk_modules_lang`.`lang_id` =  1
AND `si_bk_modules`.`action` =  'manufacturers/manufacturers/index'"
    }
    ["query_times"]=>
    array(21) {
      [0]=>
      float(0.000279903411865)
      [1]=>
      float(0.000280141830444)
      [2]=>
      float(0.000396966934204)
      [3]=>
      float(0.000326871871948)
      [4]=>
      float(0.000332832336426)
      [5]=>
      float(0.000198125839233)
      [6]=>
      float(0.000174045562744)
      [7]=>
      float(0.000277996063232)
      [8]=>
      float(0.000252962112427)
      [9]=>
      float(0.00021505355835)
      [10]=>
      float(0.000164985656738)
      [11]=>
      float(0.000163078308105)
      [12]=>
      float(0.000169038772583)
      [13]=>
      float(0.000268936157227)
      [14]=>
      float(0.000154972076416)
      [15]=>
      float(0.000160932540894)
      [16]=>
      float(0.000149965286255)
      [17]=>
      float(0.000221014022827)
      [18]=>
      float(0.000159978866577)
      [19]=>
      float(0.000267028808594)
      [20]=>
      float(0.000392913818359)
    }
    ["data_cache"]=>
    array(0) {
    }
    ["trans_enabled"]=>
    bool(true)
    ["trans_strict"]=>
    bool(true)
    ["_trans_depth"]=>
    int(0)
    ["_trans_status"]=>
    bool(true)
    ["cache_on"]=>
    bool(false)
    ["cachedir"]=>
    string(0) ""
    ["cache_autodel"]=>
    bool(false)
    ["CACHE"]=>
    NULL
    ["_protect_identifiers"]=>
    bool(true)
    ["_reserved_identifiers"]=>
    array(1) {
      [0]=>
      string(1) "*"
    }
    ["stmt_id"]=>
    NULL
    ["curs_id"]=>
    NULL
    ["limit_used"]=>
    NULL
    ["stricton"]=>
    bool(false)
  }
  ["soft_delete":protected]=>
  bool(false)
  ["soft_delete_key":protected]=>
  string(7) "deleted"
  ["_temporary_with_deleted":protected]=>
  bool(false)
  ["_temporary_only_deleted":protected]=>
  bool(false)
  ["before_create":protected]=>
  array(1) {
    [0]=>
    string(18) "protect_attributes"
  }
  ["after_create":protected]=>
  array(0) {
  }
  ["before_update":protected]=>
  array(1) {
    [0]=>
    string(18) "protect_attributes"
  }
  ["after_update":protected]=>
  array(0) {
  }
  ["before_get":protected]=>
  array(0) {
  }
  ["after_get":protected]=>
  array(0) {
  }
  ["before_delete":protected]=>
  array(0) {
  }
  ["after_delete":protected]=>
  array(0) {
  }
  ["callback_parameters":protected]=>
  array(0) {
  }
  ["protected_attributes":protected]=>
  array(0) {
  }
  ["belongs_to":protected]=>
  array(0) {
  }
  ["has_many":protected]=>
  array(0) {
  }
  ["_with":protected]=>
  array(0) {
  }
  ["validate":protected]=>
  array(0) {
  }
  ["skip_validation":protected]=>
  bool(false)
  ["_temporary_return_type":protected]=>
  string(5) "array"
}
michail1982 commented 10 years ago

limit method does not return results, its set limit try $this->manufacturers_model->limit($10, 0)->get_all();