Creating Records
Now, let's turn our attention to the business of inserting database records. To achieve this we'll be calling upon the model and specifically, we'll be invoking the insert() method. Full details about the model's insert method can be found at:
https://trongate.io/docs_m/information/the-insert-method
If you're a developer who prefers to watch videos, here's everything that you need to know about this topic:
[youtube]{
"headline": "Video Tutorial",
"info": "In this video, we'll take a closer look at how to create new records.",
"videoId":"JFnNUHIK2aE"}
[/youtube]
If you're the kind of developer who likes written text, here's everything that gets covered in the video:
Understanding How To Insert Records
In order to insert records, we can simply add the following two lines of PHP code.
$data = $this->_get_data_from_post();
$this->model->insert($data);
So, our complete submit method (on Books.php) might now look like this:
function submit() {
$submit = post("submit");
if ($submit == "Submit") {
$this->validation_helper->set_rules("title", "book title", "required|min_length[3]|max_length[75]");
$this->validation_helper->set_rules("description", "book description", "required");
$this->validation_helper->set_rules("author_id", "author", "required");
//run the validation tests
$result = $this->validation_helper->run(); //returns true or false
if ($result == true) {
$data = $this->_get_data_from_post();
$this->model->insert($data);
echo "record was successfully inserted";
} else {
$this->create();
}
}
}
As a reminder, here's our _get_data_from_post() method:
function _get_data_from_post() {
$data["title"] = post("title", true);
$data["description"] = post("description", true);
$data["author_id"] = post("author_id", true);
$data["published"] = post("published", true);
return $data;
}
Fixing SQL Errors With SetType
If you attempt to create a books record that has the 'published' field left unticked then _get_data_from_post() will initialise an empty string for 'published'. Unfortunately, this may produce an SQL error - since the 'published' field, on the database table, has a type of 'tinyint'.
To resolve those kinds of errors, we can use PHP's inbuilt settype() method.
The settype() method comes with all installations of PHP. It accepts two arguments:
- a variable name
- the type that we'd like the variable to be set to
For example, we could set 'published' to a type of integer with:
settype($data["published"], "integer");
So now, our submit method will be as follows:
function submit() {
$submit = post("submit");
if ($submit == "Submit") {
$this->validation_helper->set_rules("title", "book title", "required|min_length[3]|max_length[75]");
$this->validation_helper->set_rules("description", "book description", "required");
$this->validation_helper->set_rules("author_id", "author", "required");
//run the validation tests
$result = $this->validation_helper->run(); //returns true or false
if ($result == true) {
$data = $this->_get_data_from_post();
settype($data["published"], "integer");
$this->model->insert($data);
echo "record was successfully inserted";
} else {
$this->create();
}
}
}