count() errors on custom field order by (v1.1)

Not sure if this is intended behaviour.

But an “ORDER BY” should be removed with the model’s COUNT() call.

Why is it needed?

I am ordering a result set based on a custom field in my select statement.

Running a count() triggers an error. A simple example below.




// to trigger the error

$c = new CDbCriteria;

$c->select = array('id','name as customFieldName');

$c->order = 'customFieldName ASC';


SomeModel::model()->count($c);




I’m fixing this by altering CDbCommandBuilder::createCountCommand





// FROM 

$this->ensureTable($table);

$criteria->select='COUNT(*)';

return $this->createFindCommand($table,$criteria);


// TO

$this->ensureTable($table);

$c = clone $criteria;

$c->order = '';

$c->select = 'COUNT(*)';

return $this->createFindCommand($table, $c);




thoughts? should i post a bug?

What’s the error? What’s the SQL command generated by CDbCriteria?

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘customFieldName’ in ‘order clause’

I’ve submitted a bug.

An order by should not be in a count(*) statement

$criteria->limit = 30

$criteria->offset = 5

LIke wise, should a School::model->count($criteria)

apply the offset also? When i have offset in the criteria, it generates this:

SELECT COUNT(*) FROM schools LIMIT 30 OFFSET 5

Which doesn’t return anything in mysql.