Skip to content

Model's without primary keys get pagination counts wrong #1597

Description

@lonnieezell

It seems that I encountered another issue because of not using primary key. It seems that the new CodeIgniter's design is built around primary key system?

I supposed to have '5' results from my query.
I want to count how many result for pagination so I used $query->countAllResults() to count it, the result was '2', I don't know where '2' came from.
I then tried to view the result using $query->getResult(), it shows '5' results correctly. $query->paginate() shows correct result as well.

Here's the test code I used

$query = $this->select('name, SUM(ctr_choose) as total_choose');
$query->join('food', 'user_answer.id_food=food.id');
$query->groupBy('id_food');
$compiled = $query->getCompiledSelect();
print_r($compiled);
echo '|';
$this->resetQuery();
$query = $this->select('name, SUM(ctr_choose) as total_choose');
$query->join('food', 'user_answer.id_food=food.id');
$query->groupBy('id_food');
print_r($query->countAllResults()); //result: 2 (incorrect)

echo '|';
$query = $this->query($compiled);
print_r(count($query->getResult())); //result: 5 (correct)
	
$link = mysqli_connect("localhost", "root", "", "battermod");
$result = mysqli_query($link, $compiled);
$num_rows = mysqli_num_rows($result);
echo "\n <$num_rows Rows>\n"; //result: 5 (correct)

Because there's no $this->db->last_query();, I don't know what kind of query countAllResults produced.

Originally posted by @cobafesbuk in #1583 (comment)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    databaseIssues or pull requests that affect the database layer

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions