The Trongate PHP Framework
Documentation
Introduction
Quick Start
Basic Concepts
Understanding Routing
Controllers
Views
Assets
Modules Calling Modules
Parent & Child Modules
Database Operations
Modules within Modules
Templates & Themes
Helpers Explained
Form Handling
Working with Files
The Module Import Wizard
Authorization & Authentication
The API Explorer
Best Practices

Help Improve Our Docs

If you’ve found an error, spotted something missing, or feel a section could be clearer or better explained, we’d love to hear from you. Your feedback helps keep the documentation accurate and useful for everyone.

Please report issues or suggest improvements on GitHub. Community input is invaluable in making the docs stronger.

Not comfortable with GitHub? No problem — you can also get in touch with us directly via our contact form. We welcome all feedback.

Data Manipulation

The Trongate Model class provides the following methods for inserting, updating, and deleting database data:

  • insert()
  • update()
  • update_where()
  • delete()
  • insert_batch()

Creating Database Records With The Insert Method

The insert() method inserts a row of data into a database table. With this method, rows of new data are represented by a PHP array. This method accepts the following parameters:

  • $data (required) - an array of key-value pairs representing the table row that is to be inserted.
  • $target_table (optional) - the name of the database table where the new row is to be inserted. By default, the Model will assume the target table is equal to the value in the first URL segment.

What Gets Returned?

If the method results in a new table row being inserted, the 'id' of the newly inserted record will be returned.

Example

The syntax below shows the simplest insert example possible. In this example, we create an array of data that represents a new table row. Then, we invoke the insert method to add a new row to a database table using the array we created.

The code above will produce the following SQL query:

INSERT INTO tablename (first_name, last_name, trongate_user_id) VALUES ('Eric', 'Clapton', 91)

In a working example, 'tablename' would be replaced with the first segment from the URL.

Updating Database Records With The Update Method

The update() method updates a record in a database table. This method takes the ID of the record to update, an associative array containing column names as keys and their corresponding values, and an optional parameter specifying the name of the database table to update. It constructs and executes an SQL query to perform the update, returning true if the update was successful and false otherwise. This method accepts the following parameters:

  • $update_id (required) - the ID of the record to update.
  • $data (required) - an associative array containing column names as keys and their corresponding values.
  • $target_table (optional) - the name of the database table to update. By default, the Model will assume the target table is equal to the value in the first URL segment.

What Gets Returned?

If the method successfully updates the record, it returns true. Otherwise, it returns false.

Example

The code sample below demonstrates how to use the update() method to update a record in the database.

Deleting Database Records With The Delete Method

The delete() method deletes a record from a database table based on its ID. This method takes the ID of the record to delete and an optional parameter specifying the name of the database table. It constructs and executes an SQL query to delete the record, returning true if the delete operation was successful and false otherwise. This method accepts the following parameters:

  • $id (required) - the ID of the record to delete.
  • $target_table (optional) - the name of the database table. By default, the Model will assume the target table is equal to the value in the first URL segment.

What Gets Returned?

If the method successfully deletes the record, it returns true. Otherwise, it returns false.

Example

The code sample below demonstrates how to use the delete() method to delete a record from the database.

Video Tutorial: Creating, Updating & Deleteing Records

The video below demonstrates how how to create, update and delete database records using Trongate's Model class.

×