Concept of Query Grouping in CodeIgniter 4 Tutorial

Share this Article
Reading Time: 5 minutes
3,871 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 –

Let’s get started.


CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


.env Setup

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

Either we can do via renaming file as simple as that. Also we can do by terminal command.

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.

Enable Development Mode

CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.

Open .env file from root.

# 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.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5