Saturday

Instant CRUD—or Putting it

Instant CRUD—or Putting it
All Together

The most essential—and the most boring—part of writing any dynamic site is the
CRUD. You have one or more database tables; you need to be able to Create, Read,
Update, and Delete entries on each of these. Later on, you'll do clever things with the
data, but until there is some user-friendly way to put it there and maintain it, your
site isn't viable.

But this involves writing CRUD functions and these, though conceptually quite easy,
are fairly complex and time-consuming. So for our site, I've written a generalized
CRUD model, using CI's classes and helpers to make it easier. In this chapter, you'll
see how this model works and how to integrate it into our application.

The CRUD model does more than just CRUD. It validates user-entered data, and also
checks it in several ways—e.g., to see that you don't do a 'delete' operation without
limiting it to a specific table row, or that you don't accidentally repeat 'create'
operations by going back to the entry form and reloading it in your browser.

Lastly, the CRUD model contains its own self-testing framework, so that you can
perform development tests as you build or adapt your code.

There are other CRUD frameworks out there, which may be more comprehensive
and possibly better code (see Chapter 15). However, this one does work. And it's a
good way of summing up and using many of the lessons that we've learned in the
previous chapters.

This chapter sets out the code for the model, with some comments:

• First of all, we look at the design philosophy.

• Then we look at a standard controller to use with the model.

• Then we look at the way database tables must be structured.