11. Controller CRUD

We just generated a lot of code to create the CRUD functions. Let's explore and see how it all works.

First we will look at the Read function as displayed the index.php view script and the view.php view script. The index.php view in this case is a grid view listing all the data in the database. Each row in the grid is a record. The view.php view is listing all the data for a specific record.

Next, we will look at Creating records and Updating records, which will involve exploiting the Yii Active Form functionality.

And finally there is the delete function to remove a record.

But, before we go there, let's just see how the CRUD controller works fundamentally.

11.1 CRUD Basics

Let's take a couple very simple examples to demonstrate how easy it is to interact with the database using Active Record.

11.1.1 CRUD Create

Let's say we want to insert a new record in the Customers table and set the value for the field contactFirstName as Joe.

Here is the code you would use in the controller:

$customers = new Customers();
$customers->contactFirstName = 'Joe';
$customers->save();

In this example, we are creating a new Customers object, setting the contactFirstName as Joe and saving the record.

And that is all there is to it. Simple.

However, as written, that will not work. Why? Because of the rules.

Remember this element of the Rules array?

[['companyName', 'contactLastName', 'contactFirstName', 'phone', 'addressLine1', 'city', 'country'], 'required']

In addition to the contactFirstName field being required, the rule above stipulates that the following fields also contain a value:

  • companyName
  • contactLastName
  • phone
  • addressLine1
  • city
  • country

However, we are trying to insert a record with only the contactFirstName attribute set (remember, model attributes correlate to table fields). When you call the save() function for a model, it automatically validates the input against the rule set for that model.

Thus, we have two options.

  1. Supply values for the rest of the required attributes.
  2. Disable validation for this instance.

Fortunately, disabling validation is easy. You just pass a 'false' value to the save() function. You would write it like this:

$customers->save(false);

Doing so will allow you to execute the save() function and write a record to the database table with just the contactFirstName field supplied.

11.1.2 CRUD Read

How would we go about getting the record we just inserted?

To demonstrate we will agree that the customerNumber in the table is the primary key and it is auto increment. The record we just inserted is the first one, so the customerNumber for the record we just inserted is 1.

Here is the code we would use in the controller to pull that record:

$customer = Customers::findOne(1);

Now, if we want to echo out the value for the contactFirstName attribute, you would do so as such:

echo $customer->contactFirstName;

This is cheating a bit in that according to the MVC paradigm, we should not be echoing out anything in a controller. Only views should be used for writing to the browser. But, if you did, it would work. Just don't do it. Really.

11.1.3 CRUD Update

Now, Joe has become transgender and we want to change his name to Josephine. Here is the code:

$customer = Customers::findOne(1);
$customer->contactFirstName = 'Josephine';
$customer->update(false);

First we are fetching the record with primary key 1, which has the attribute contactFirstName set to Joe, as stated above. We are assigning the attribute to 'Josephine' and we are updating the record.

Note that we are again passing 'false' to the update() function so that validation will be disabled for this event.

Also, we could have used the save() function just as well. Even though it is an update, the save() function will work. save() checks if the record exists and automatically does an update if it does. We are just saving Yii a step by using update().

11.1.4 CRUD Delete

Now, Josephine feels we have not been sensitive enough to her transformation and decided to no longer be our customer. Here is how we would delete that record:

$customer = Customers::findOne(1);
$customer->delete();

Here we fetch the record, create an object to represent it and execute the delete() function on it. Poof! The record is gone from the table. Goodbye Joe, or Josephine, or whoever.