Kohana CRUD – Or How to Create, Update, and Delete From the Database with ORM

jsandahl

ORM::factory has become a good friend as of the last few weeks. Once you understand the basic idea behind queries, you know they can be reduced down to CRUD. That is, you will be Creating, Updating, or Deleting database rows. This post will cover how to do each one of those simply with ORM in Kohana.

Most of the time, your database will be updated based on form submissions, so this is an appropriate place to cover how Kohana deals with $_POST. When you pass your $variable to the controller, you can catch it inside the appropriate actions (I like to name them something like <user>/<create> so I know what’s supposed to happen within that action. To catch the $_POST variables, you will probably need to check each one to make sure it is set and then declare it. Here’s a sample:

if(isset($this->request->post('field_name')))
{
$field_name = $this->request->post('field_name');
}

As you can see, you need to use Kohana’s $this->request->post() to capture post variables. So far, so good.

When it comes to creating a new entry, we’re still not too involved. First, you’ll pass an instance of the table (which you’ve hopefully declared in the model already. Otherwise, you’ll need to create a model or you’ll get a wonderful reminder that the model doesn’t exist.) to a variable.

$users = ORM::factory('users');

Now is when it really gets exciting. You’ll take your variable and change the content of each row with the column you call. It really couldn’t get easier than to do it this way. You don’t have to worry about quotation marks or other punctuation. Just remember the fieldname and the variable or info you want to put in the row under that column.

$users->first_name = $firstname;

And here’s the clincher. What seals the deal is the very simplistic calling the save function, which runs the query. That’s it! You’ve now created your first entry in the database using Kohana. You deserve a pat on the back.

$users->save();

Now for the update, the only part that changes is where you call the instance. You’ll need to know the id (in most cases) of the field you’re selecting, but the update is very much identical once you’ve selected the correct row. So you’ll pass in your unique id to the instance you call. (Once again, this will only work if you’ve declared your model correctly. Once I forgot the name of the column and put ‘id’ instead of ‘user_id’ and it didn’t work. But I’m sure you won’t do that…I digress.) So this is all you need to do:

$users = ORM::factory('users', $unique_id);

Then add whatever you need to the rows:

$users->first_name = $firstname;
$users->last_name = $lastname;

Then save it:

$users->save();
Super easy, right!

So now all you need is to be able to delete what you need to. Deleting entries should be done only sparsely, because you don’t want to eliminate things from the database if you can help it, since databases provide record of what happened. Besides, changes to the database are usually pretty irreversible.Nevertheless, Kohana doesn’t make it difficult for you to delete your rows. For each loaded row (if you have more than one, you can foreach($items as $item) through them.) Here it is!

$users->delete();

So now you know how to Create, Update and Delete using Kohana’s ORM.

Warning. This is known to cause addiction in developers with a propensity towards doing things quickly, efficiently, and easily.

What Are Your Thoughts?

Your email address will not be published. Required fields are marked *