Book Image

CodeIgniter 1.7 Professional Development

By : Adam Griffiths
Book Image

CodeIgniter 1.7 Professional Development

By: Adam Griffiths

Overview of this book

<p>CodeIgniter is an open source PHP framework with a small footprint and exceptional performance. It gives you a rich set of libraries for common tasks, with a simple interface to access them. There are several unexplored aspects of CodeIgniter that can help developers build applications more easily and quickly. In this book, you will learn the intricacies of the framework and explore some of its hidden gems.<br /><br />If you want to get the most out of CodeIgniter, this book is for you. It teaches you what you need to know to use CodeIgniter on a daily basis. You will create mini-applications that teach a specific technique and let you build on top of the base. <br /><br />This book will take you through developing applications with CodeIgniter. You will learn how to make your CodeIgniter application more secure than a default installation, how to build large-scale applications and web services, how to release code to the community, and much more. You will be able to authenticate users, validate forms, and also build libraries to complete different tasks and functions.<br /><br />The book starts off introducing the framework and how to install it on your web server or a local machine. You are introduced to the Model-View-Controller design pattern and how it will affect your development. Some important parts of the CodeIgniter Style Guide are included to keep CodeIgniter development as standardized as possible; this helps greatly when working as part of a team or taking on an old CodeIgniter project. You will quickly move on to how CodeIgniter URLs work and learn about CodeIgniter-specific files such as helpers and plugins. By the time you finish this book, you will be able to create a CodeIgniter application of any size with confidence, ease, and speed.</p>
Table of Contents (16 chapters)
CodeIgniter 1.7 Professional Development
Credits
About the Author
About the Reviewers
Preface
Index

Models: Data abstraction layer


Now that you know what a Model is and what it's used for, we can take a look at how to construct a Model.

Models are stored in system/application/models/. The class of your model should have the first letter capitalized and the rest of the name lowercase, and also extend the base class of Model. The class should also have a constructor that calls upon the base class constructor, as seen in the next code example.

<?php
class Mymodel extends Model
{
  funcion Mymodel()
  {
    parent::Model();
  }
}
?>

The filename of this model should be an all-lowercase version of the name; the filename therefore will be mymodel.php.

Loading a Model

A model is loaded from Controller functions. Loading a model takes just one line of code.

$this->load->model('model_name');

In this instance, you should replace model_name with the name of your model. If your model is located in a sub-folder, you would load the model as follows:

$this->load->model('sub_folder/model_name');

Once loaded, you access your model functions by using the global object with the same name as your model name.

$this->model_name->function_name();

You can assign a new name for your Model object by passing it to the second parameter of the loading function.

$this->load->model('model_name', 'different_name');

Now you would call your model functions as follows:

$this->different_name->function_name();

Here's an example of a controller calling a model and serving a view.

<?php
class Shop extends Controller
{
  function Shop()
  {
    parent::Controller();
  }

  function index()
  {
    $this->load->model('products');
    $products = $this->products->build_list();
    $this->load->view('shop', $products);
  }
}
?>

Connecting to your database automatically

When a model is loaded, it does not connect to the database automatically unless you autoload the database class for your application. You can tell the model to connect to the database automatically by passing boolean TRUE as the third parameter.

$this->load->model('model_name', '', TRUE);

The model will now connect to the database using the setting defined in your database.php configuration file, which is located in system/application/config/.

You can pass an array of configuration details to the third parameter if you wish to use a database other than the one that's set up in your database configuration file.

$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);