Concept of Query Grouping in CodeIgniter 4 Tutorial

Share this Article
Reading Time: 5 minutes
247 Views

CodeIgniter 4 provides the complete set of Query builder methods to use in querying database. Inside this article we will see the concept of query grouping in codeigniter 4 Query builder.

In Query builder to use query grouping we have a method available. Query grouping allows us to create groups of WHERE clauses by enclosing them in parentheses. This will allow us to create queries with complex WHERE clauses. Nested groups are supported.

Query grouping helps to create nested queries with multiple conditions using OR and AND.

Learn More –

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

Let’s get started.


Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system. To set application we have multiple options to proceed.

Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer –

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Turn Development Mode On

When we install CodeIgniter 4, we have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Open project in terminal

$ cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.

# CI_ENVIRONMENT = production

// Do it to 

CI_ENVIRONMENT = development

Now application is in development mode.


Query Grouping Methods of CodeIgniter 4

Query grouping allows us to create groups of WHERE clauses by enclosing them in parentheses. This will allow us to create queries with complex WHERE clauses. Nested groups are supported.

Following are the methods available –

  • groupStart()
  • orGroupStart()
  • groupEnd()
  • notGroupStart()
  • havingGroupStart()

Inside this article we will see the concept of first three given methods groupStart(), orGroupStart() and groupEnd().

groupStart()

Starts a new group by adding an opening parenthesis to the WHERE clause of the query.

orGroupStart()

Starts a new group by adding an opening parenthesis to the WHERE clause of the query, prefixing it with ‘OR’.

groupEnd()

Ends the current group by adding a closing parenthesis to the WHERE clause of the query.


Examples of Query Grouping

Here, we will see few examples of query in query builder.

Create Query Builder Instance

//...

$this->db = db_connect();

$builder = $this->db->table("students");

//...

Example #1

We want to create a group of query in which conditions be like

Condition 1 AND ( Condition 2 OR Condition 3 )

MySQL Query

SELECT * FROM `my_table` WHERE `column1` = 'a'  AND ( `column2` = 'b' OR `column3` = 'c' ) 

Writing in CodeIgniter 4

We will use query builder methods for this.

<?php

namespace App\Controllers;

class Home extends BaseController
{
	public function __construct()
	{
		$this->db = db_connect();
	}

	public function index()
	{
		$builder = $this->db->table("my_table");

		$query = $builder->select('*')
		        ->groupStart()
		            ->where('column1', 'a')
		            ->GroupStart()
		                ->where('column2', 'b')
		                ->orWhere('column3', 'c')
		            ->groupEnd()
		        ->groupEnd()
		    ->get();

		echo "<pre>";
		print_r($query->getResult());

		// To get last executed query
		// $this->db->getLastQuery();
	}
    
    //...
}

Behind the scene it will generate query as –

SELECT * FROM `my_table` WHERE ( `column1` = 'a' AND ( `column2` = 'b' OR `column3` = 'c' ) )

Example #2

We want to create a group of query in which conditions be like

( Condition 1 AND Condition 2 ) OR ( Condition 3 AND Condition 4 )

MySQL Query

SELECT * FROM `my_table` WHERE  (`column1` = 'a'  AND `column2` = 'b' )  OR ( `column3` = 'c' AND `column4` = 'd' ) 

Writing in CodeIgniter 4

We will use query builder methods for this.

<?php

namespace App\Controllers;

class Home extends BaseController
{
	public function __construct()
	{
		$this->db = db_connect();
	}

	public function index()
	{
		$builder = $this->db->table("my_table");

		$query = $builder->select('*')
		        ->groupStart()
		            ->where('column1', 'a')
                    ->where('column2', 'b')
                ->groupEnd()
		        ->orGroupStart()
		                ->where('column3', 'c')
		                ->where('column4', 'd')
		        ->groupEnd()
		    ->get();

		echo "<pre>";
		print_r($query->getResult());

		// To get last executed query
		// $this->db->getLastQuery();
	}
    
    //...
}

Behind the scene it will generate query as –

SELECT * FROM `my_table` WHERE ( `column1` = 'a' AND `column2` = 'a' ) OR ( `column3` = 'c' AND `column4` = 'd' )

We hope this article helped you to learn Concept of Query Grouping in CodeIgniter 4 Tutorial in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on CodeIgniter 4 here

Leave a Comment