Trongate Docs
switch to dark modeswitch to dark mode
»
»
The 'Get' Method

The 'Get' Method

The 'Get' method is for fetching rows of data from a database table. It accepts the following parameters:

  • $order_by (optional) - the name of the table column by which results should be ordered. In instances where you'd like your results to be displayed in descending order, add the characters ' desc' onto your order by argument. By default, the Model will order results by 'id'.
  • $target_table (optional) - the name of the database table to be queried. By default, the Model will assume the target table to be equal to the value on the first URL segment.
  • $limit (optional) - the maximum number of results that you would like to return. By default, the Model will assume no limit.
  • $offset (optional) - the number of rows to skip before fetching results.

Just To Let You Know
The starting point for a database results set is always assumed to have an offset of 0.

What Gets Returned?

WHEN MATCHING RECORDS ARE FOUND

If matching records are found then a PHP array will be returned where each item on the array represents a row from a database table. Each row, within the array, will be a PHP object.

WHEN MATCHING RECORDS ARE NOT FOUND

If the query fails to find any matching records then an empty array will be returned.

Example 1

The syntax below shows the simplest Get example possible.  In this example, we pass no arguments in and the Model will use default values, as defined above, to generate a database query.

$rows = $this->model->get();

The code above will produce the following SQL query:

SELECT * from tablename ORDER BY id

Just To Let You Know
In a working example, 'tablename' would be replaced with the first segment from the URL.

Below is an example of a Members.php controller file that contains a test() method.  Here we're invoking a basic 'Get' command and then immediately displaying the results using Trongate's json() method:

<?php
class Members extends Trongate {

    function test() {
        $rows = $this->model->get();
        json($rows);
    }

Below shows an example of the kind of output that we can expect to see from the above method, when Debug Mode is switched on:

screenshot

Example 2

Below is a more advanced example:

$rows = $this->model->get('trongate_user_id desc', 'members', 3, 0);

The code above would produce the following SQL query:

SELECT * FROM members order by trongate_user_id desc LIMIT 0, 3



HELP & SUPPORT

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

 
×