Trongate Docs
switch to dark modeswitch to dark mode
»
»
Creating Records

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:

Video Tutorial

In this video, we'll take a closer look at how to create new records.


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);

Just To Let You Know
This code is presented with the assumption that you have created a _get_data_from_post() method, as explained here.

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();
​    }
​  }
​}​

Just To Let You Know
When a non-integer, such as an empty string, is passed through settype() and set to 'integer' then the value of the non-integer will be changed to a 0 (a zero).



HELP & SUPPORT

If you have a question or a comment relating to anything you've see here, please goto the Help Bar.

 
×