CodeIgniter Forums
CI 4.2 countAllResults on Union Queries - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: CI 4.2 countAllResults on Union Queries (/showthread.php?tid=87676)



CI 4.2 countAllResults on Union Queries - Jake Doran - 05-18-2023

Hello all!

I'm a long time CodeIgniter user and first time poster.

I've recently inherited a project that has been migrated from CI 3 and PHP 5 to CI 4.2 and PHP 7.4. I've noticed a number of new features in 4.2 that I am not yet familiar with as I come from a CI 3 background.

One such feature that I've been trying to use is the Union Query in combination with the BaseBuilder countAllResults method.

However, when I try to do this it seems as if the select statement that gets injected by countAllResults is only being added to one side of the union meaning that an Exception is thrown because the two queries are not equal. I wanted to check if perhaps my approach / understanding is wrong before potentially raising a bug on the GitHub project.

So the gist of the code is as follows...

In a model a query is being built and then cloned to add additional conditionals.

PHP Code:
$union = clone $this->builder(); 
PHP Code:
$union->where("my_field"$myVar); 

The model is then returning a BaseBuilder instance:

PHP Code:
return $this->builder()->union($union

This is then passed off to a custom pagination class that takes the BaseBuilder instance and does the following:

PHP Code:
$builder->countAllResults(false); 

This is then throwing the following Exception:

mysqli_sql_exception #1222

The used SELECT statements have a different number of columns.


Has anyone experienced anything like this before / can spot any obvious mistakes or incorrect assumptions?

Thanks in advance!

- Jake


RE: CI 4.2 countAllResults on Union Queries - kenjis - 06-17-2023

Check the generated SQL statement.