propelorm / Propel2

Propel2 is an open-source high-performance Object-Relational Mapping (ORM) for modern PHP
http://propelorm.org/
MIT License
1.26k stars 399 forks source link

Cannot find selected column XXXX when use pagination and previously set a withColumn custom column for the select statement #1999

Open c15k0 opened 7 months ago

c15k0 commented 7 months ago

Taking this example:

$query = BookQuery:create()->withColumn('now()', 'exampleCurrentTs');
$results = $query->paginate(1, 50);

It throws an exception like: throw new PropelException("Cannot find selected column '$columnName'"); in ModelCriteria.php at line 2389

What is the reason?, in order to create the count statement, in the Query classes there are some instructions that have to clear the selectColumns and asColumns arrays in order to add only the count(*) to the statement:

// Replace SELECT columns with COUNT(*)
$this->clearSelectColumns()->addSelectColumn('COUNT(*)');
$sql = $this->createSelectSql($params);

Everybody could find these three lines (including the comment) in the doCount method into every Query classes.

The problem is that the clearSelectColumns method from the Criteria class do this:

public function clearSelectColumns()
{
    $this->selectColumns = $this->asColumns = [];
    return $this;
}

instead of this:

public function clearSelectColumns()
{
    $this->selectColumns = $this->asColumns = $this->select = [];
    return $this;
}

And it causes the exception because the withColumn still being into the select when try to verify all the fields before doing the count.

I tried with several examples this change and it only affects to the count statements, so it could be a good fix for that.

Thanks