From a1360ef24fff8b57353db32ad6045969af28e5d5 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Wed, 5 Oct 2011 17:22:53 -0500 Subject: fixing code spacing in Profiling, Models, Managing Apps, Hooks, and Helpers general docs --- user_guide_src/source/general/helpers.rst | 23 +++++- user_guide_src/source/general/hooks.rst | 24 ++++++- user_guide_src/source/general/managing_apps.rst | 15 +++- user_guide_src/source/general/models.rst | 95 +++++++++++++++++++++---- user_guide_src/source/general/profiling.rst | 10 ++- 5 files changed, 149 insertions(+), 18 deletions(-) (limited to 'user_guide_src/source') diff --git a/user_guide_src/source/general/helpers.rst b/user_guide_src/source/general/helpers.rst index 2b113c1f4..71cb8b25a 100644 --- a/user_guide_src/source/general/helpers.rst +++ b/user_guide_src/source/general/helpers.rst @@ -102,7 +102,28 @@ For example, to extend the native Array Helper you'll create a file named application/helpers/MY_array_helper.php, and add or override functions:: - // any_in_array() is not in the Array Helper, so it defines a new function function any_in_array($needle, $haystack) {     $needle = (is_array($needle)) ? $needle : array($needle);     foreach ($needle as $item)     {         if (in_array($item, $haystack))         {             return TRUE;         }         }     return FALSE; } // random_element() is included in Array Helper, so it overrides the native function function random_element($array) {     shuffle($array);     return array_pop($array); } + // any_in_array() is not in the Array Helper, so it defines a new function + function any_in_array($needle, $haystack) + { + $needle = (is_array($needle)) ? $needle : array($needle); + + foreach ($needle as $item) + { + if (in_array($item, $haystack)) + { + return TRUE; + } + } + + return FALSE; + } + + // random_element() is included in Array Helper, so it overrides the native function + function random_element($array) + { + shuffle($array); + return array_pop($array); + } Setting Your Own Prefix ----------------------- diff --git a/user_guide_src/source/general/hooks.rst b/user_guide_src/source/general/hooks.rst index ab42d28a1..65696f6c7 100644 --- a/user_guide_src/source/general/hooks.rst +++ b/user_guide_src/source/general/hooks.rst @@ -26,7 +26,13 @@ Defining a Hook Hooks are defined in application/config/hooks.php file. Each hook is specified as an array with this prototype:: - $hook['pre_controller'] = array(                                 'class'    => 'MyClass',                                 'function' => 'Myfunction',                                 'filename' => 'Myclass.php',                                 'filepath' => 'hooks',                                 'params'   => array('beer', 'wine', 'snacks')                                 ); + $hook['pre_controller'] = array( + 'class' => 'MyClass', + 'function' => 'Myfunction', + 'filename' => 'Myclass.php', + 'filepath' => 'hooks', + 'params' => array('beer', 'wine', 'snacks') + ); **Notes:** The array index correlates to the name of the particular hook point you @@ -54,7 +60,21 @@ Multiple Calls to the Same Hook If want to use the same hook point with more then one script, simply make your array declaration multi-dimensional, like this:: - $hook['pre_controller'][] = array(                                 'class'    => 'MyClass',                                 'function' => 'Myfunction',                                 'filename' => 'Myclass.php',                                 'filepath' => 'hooks',                                 'params'   => array('beer', 'wine', 'snacks')                                 ); $hook['pre_controller'][] = array(                                 'class'    => 'MyOtherClass',                                 'function' => 'MyOtherfunction',                                 'filename' => 'Myotherclass.php',                                 'filepath' => 'hooks',                                 'params'   => array('red', 'yellow', 'blue')                                 ); + $hook['pre_controller'][] = array( + 'class' => 'MyClass', + 'function' => 'Myfunction', + 'filename' => 'Myclass.php', + 'filepath' => 'hooks', + 'params' => array('beer', 'wine', 'snacks') + ); + + $hook['pre_controller'][] = array( + 'class' => 'MyOtherClass', + 'function' => 'MyOtherfunction', + 'filename' => 'Myotherclass.php', + 'filepath' => 'hooks', + 'params' => array('red', 'yellow', 'blue') + ); Notice the brackets after each array index:: diff --git a/user_guide_src/source/general/managing_apps.rst b/user_guide_src/source/general/managing_apps.rst index edc94d284..996481354 100644 --- a/user_guide_src/source/general/managing_apps.rst +++ b/user_guide_src/source/general/managing_apps.rst @@ -39,7 +39,20 @@ inside your application folder into their own sub-folder. For example, let's say you want to create two applications, "foo" and "bar". You could structure your application folders like this:: - applications/foo/ applications/foo/config/ applications/foo/controllers/ applications/foo/errors/ applications/foo/libraries/ applications/foo/models/ applications/foo/views/ applications/bar/ applications/bar/config/ applications/bar/controllers/ applications/bar/errors/ applications/bar/libraries/ applications/bar/models/ applications/bar/views/ + applications/foo/ + applications/foo/config/ + applications/foo/controllers/ + applications/foo/errors/ + applications/foo/libraries/ + applications/foo/models/ + applications/foo/views/ + applications/bar/ + applications/bar/config/ + applications/bar/controllers/ + applications/bar/errors/ + applications/bar/libraries/ + applications/bar/models/ + applications/bar/views/ To select a particular application for use requires that you open your main index.php file and set the $application_folder variable. For diff --git a/user_guide_src/source/general/models.rst b/user_guide_src/source/general/models.rst index e26207cc2..55081d12a 100644 --- a/user_guide_src/source/general/models.rst +++ b/user_guide_src/source/general/models.rst @@ -20,10 +20,46 @@ blog. You might have a model class that contains functions to insert, update, and retrieve your blog data. Here is an example of what such a model class might look like:: - class Blogmodel extends CI_Model {     var $title   = '';     var $content = '';     var $date    = '';     function __construct()     {         // Call the Model constructor         parent::__construct();     }          function get_last_ten_entries()     {         $query = $this->db->get('entries', 10);         return $query->result();     }     function insert_entry()     {         $this->title   = $_POST['title']; // please read the below note         $this->content = $_POST['content'];         $this->date    = time();         $this->db->insert('entries', $this);     }     function update_entry()     {         $this->title   = $_POST['title'];         $this->content = $_POST['content'];         $this->date    = time();         $this->db->update('entries', $this, array('id' => $_POST['id']));     } } + class Blogmodel extends CI_Model { -Note: The functions in the above example use the :doc:`Active -Record <../database/active_record>` database functions. + var $title = ''; + var $content = ''; + var $date = ''; + + function __construct() + { + // Call the Model constructor + parent::__construct(); + } + + function get_last_ten_entries() + { + $query = $this->db->get('entries', 10); + return $query->result(); + } + + function insert_entry() + { + $this->title = $_POST['title']; // please read the below note + $this->content = $_POST['content']; + $this->date = time(); + + $this->db->insert('entries', $this); + } + + function update_entry() + { + $this->title = $_POST['title']; + $this->content = $_POST['content']; + $this->date = time(); + + $this->db->update('entries', $this, array('id' => $_POST['id'])); + } + + } + +.. note:: The functions in the above example use the :doc:`Active + Record <../database/active_record>` database functions. .. note:: For the sake of simplicity in this example we're using $_POST directly. This is generally bad practice, and a more common approach @@ -38,7 +74,13 @@ nested within sub-folders if you want this type of organization. The basic prototype for a model class is this:: - class Model_name extends CI_Model {     function __construct()     {         parent::__construct();     } } + class Model_name extends CI_Model { + + function __construct() + { + parent::__construct(); + } + } Where Model_name is the name of your class. Class names **must** have the first letter capitalized with the rest of the name lowercase. Make @@ -47,7 +89,13 @@ sure your class extends the base Model class. The file name will be a lower case version of your class name. For example, if your class is this:: - class User_model extends CI_Model {     function __construct()     {         parent::__construct();     } } + class User_model extends CI_Model { + + function __construct() + { + parent::__construct(); + } + } Your file will be this:: @@ -71,17 +119,32 @@ application/models/blog/queries.php you'll load it using:: Once loaded, you will access your model functions using an object with the same name as your class:: - $this->load->model('Model_name'); $this->Model_name->function(); + $this->load->model('Model_name'); + + $this->Model_name->function(); If you would like your model assigned to a different object name you can specify it via the second parameter of the loading function:: - $this->load->model('Model_name', 'fubar'); $this->fubar->function(); + $this->load->model('Model_name', 'fubar'); + + $this->fubar->function(); Here is an example of a controller, that loads a model, then serves a view:: - class Blog_controller extends CI_Controller {     function blog()     {         $this->load->model('Blog');         $data['query'] = $this->Blog->get_last_ten_entries();         $this->load->view('blog', $data);     } } + class Blog_controller extends CI_Controller { + + function blog() + { + $this->load->model('Blog'); + + $data['query'] = $this->Blog->get_last_ten_entries(); + + $this->load->view('blog', $data); + } + } + Auto-loading Models =================== @@ -109,9 +172,17 @@ database. The following options for connecting are available to you: $this->load->model('Model_name', '', TRUE); - You can manually pass database connectivity settings via the third - parameter: - :: - - $config['hostname'] = "localhost"; $config['username'] = "myusername"; $config['password'] = "mypassword"; $config['database'] = "mydatabase"; $config['dbdriver'] = "mysql"; $config['dbprefix'] = ""; $config['pconnect'] = FALSE; $config['db_debug'] = TRUE; $this->load->model('Model_name', '', $config); + parameter:: + + $config['hostname'] = "localhost"; + $config['username'] = "myusername"; + $config['password'] = "mypassword"; + $config['database'] = "mydatabase"; + $config['dbdriver'] = "mysql"; + $config['dbprefix'] = ""; + $config['pconnect'] = FALSE; + $config['db_debug'] = TRUE; + + $this->load->model('Model_name', '', $config); diff --git a/user_guide_src/source/general/profiling.rst b/user_guide_src/source/general/profiling.rst index 60ef585ef..28c1dd7b8 100644 --- a/user_guide_src/source/general/profiling.rst +++ b/user_guide_src/source/general/profiling.rst @@ -48,13 +48,19 @@ application/config/profiler.php config file. :: - $config['config']          = FALSE; $config['queries']         = FALSE; + $config['config'] = FALSE; + $config['queries'] = FALSE; In your controllers, you can override the defaults and config file values by calling the set_profiler_sections() method of the :doc:`Output class <../libraries/output>`:: - $sections = array(     'config'  => TRUE,     'queries' => TRUE     ); $this->output->set_profiler_sections($sections); + $sections = array( + 'config' => TRUE, + 'queries' => TRUE + ); + + $this->output->set_profiler_sections($sections); Available sections and the array key used to access them are described in the table below. -- cgit v1.2.3-24-g4f1b