Welcome Guest, Not a member yet? Register   Sign In
CI 4.2 countAllResults on Union Queries
#1
Question 
(This post was last modified: 05-18-2023, 01:45 AM by Jake Doran.)

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
Reply
#2

Check the generated SQL statement.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB