Object Relational Mapping (ORM)

While CI provides the model class for the developer to expand for object-oriented database CRUD (Create, Read, Update, and Delete) validation, and business logic for the defined project database, there's another option that enables automatic model services. In this section, we will discuss Object Relational Mapping (ORM). ORM is a new concept of converting the database scheme definition into an object-oriented database class API. It provides database CRUD services on a given database, so that the minimal code is required, instead of the full model development. More than that, the customized validation on the CRUD operation is enabled as well. Using an ORM plugin may reduce the need to self-develop our own CI models so that only special business logic activities are left to be implemented.

Today, ORM plugins provide predefined validation services, as well as user-defined services to enforce validations on CRUD requests from the application controllers, libraries, or helpers requesting the database CRUD services.

There are pros and cons of using ORM. On one hand, it simplifies a lot of the database model development for the database. On there other hand, it dictates various rules on the database scheme definition, such as defining user tables for an ORM object user, or defining the auto-increment primary key field name, such as ID, and so on.

There are several ORM plugins for CI; the most well-known and well-documented ones, with a large network of community developers, are the following:

The ORM plugins also provide validation and manipulation services on the handled database fields, such as performing trimming on a string field before it is saved to the database.

Validation services include built-in validations such as valid e-mail fields, or a field that must have the same value as another field, such as fields with an account creation password retype requirement. The full scope and usage of ORM is beyond the scope of this CI book. However, it is highly recommended that you learn more about ORM and try using the referred ORM plugins and consider using them in your CI projects.

Of course, we do provide a simple usage example of adding a record to the database, and retrieving the database records using ORM in the following section:

ORM simple operations example

For example, let's say we have a user database table with the ID as the primary key auto-increment. User name, e-mail, and password are the other fields, and if we want to add a new user record to the database, we could do so with the help of the following code:

<?PHP
// We shall define the database table named users// with ID as auto-increment, username, password, and e-mail as // the other fields.
// ORM will create an user objet based on the users // table scheme. We can set the variable to this object, and use // the operational services provided by ORM for actions, such // as save, delete, update, and add.
$u = new User();
$u->username = 'A new User';
$u->password = 'shhnew1';
$u->email = '[email protected]';
// To add a new user record
if ($u->save()) {
  // if saved we have a new echo 'New User Id Opened having'$u->id. 'User Id <br/>';
  }
else {// Show why we failed to save echo
  $u->error->string;
  }
// Getting the first three users from the database
$u = new User();
$u->limit(3)->get();
// Showing the fetched users
foreach ($u as $user_rec)
{
  echo 'User Id: '. $user_rec->id . '<br/>';
  echo 'User Name: '. $user_rec->username . '<br />';
  echo 'User Email: '. $user_rec->email. '<br/>';
  }
// Get the user with Uid = 10 if any
$u = new user();
$seek_uid = 10;

$u->where('id', $seek_uid)->get();
// Check if found
if (exist ($u)){
  echo 'User Id:'.$u->id.' Name is'.$u->username. '<br />';
  }
else echo 'No user found for user ID'. $seek_uid. '<br />';

This is only a very simple usage example, while ORM today provides a rich set of CRUD and validation services. Please refer to the provided links to the featured ORM plugins for more information.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.59.145