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

The 'Get One Where' Method

The 'Get One Where' method is for fetching a single record from a database table where a condition is met.  It accepts the following parameters:

  • $column (required) - the name of the table column referred to when results are fetched.
  • $value (required) - the value that should be matched against the target table column.
  • $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.  

What Gets Returned?

WHEN A MATCHING RECORD IS FOUND

When matching record is found, a PHP object will be returned where each object property key represents a column from the target database table, and each object property value represents a value that is assigned to the fetched object key.

WHEN A MATCHING RECORD IS NOT FOUND

If the query fails to find any matching records then the boolean value of false is returned.

Top Tip
Use PHP's inbuilt var_dump() method whenever you need to clarify the variable types from a given results set.

Example 1

The syntax below shows the simplest Get Where example possible.  In this example, we pass a record 'id' as an argument and the Model will query the table whose name matches the first segment from the URL.

$result = $this->model->get_one_where('trongate_user_id', 90); 

The code above will produce the following SQL query:

SELECT * FROM tablename where trongate_user_id = 90

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 Where' command and then immediately displaying the result using Trongate's json() method:

<?php
class Members extends Trongate {

    function test() {
        $result = $this->model->get_one_where('trongate_user_id', 90);
        json($result);
    }

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 slightly more advanced example:

$result = $this->model->get_one_where('type', 'Mackerel', 'fish');

The code above would produce the following SQL query:

SELECT * FROM fish where type = 'Mackerel'




HELP & SUPPORT

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

 
×