I yet another inexplicable problem with CDbCriteria.
Here is my code:
'condition'=>'product_master = 0',
$criteria->addCondition('modules.oem_module_id = ' . (int)$_GET['mid']);
$criteria->addCondition('systems.oem_system_id = ' . (int)$_GET['sid']);
$pages = new CPagination(Product::model()->count($criteria));
$products = Product::model()->findAll($criteria);
As can be seen, I have three tables: products, systems(alias declared in relations), modules (alias declared in relations).
Product::model()->count($criteria) executes a perfect query with the expected joins.
Product::model()->findAll($criteria) fails. The joins are missing, thus systems.oem_system_id or modules.oem_module_id are declared as "Column not found".
What am I doing wrong??
The only way I can get this to work is to use the following:
$products = Product::model()->with('systems','modules')->together()->findAll($criteria);
To me, this looks like a bug, since the relations are already declared in $criteria and work just fine with the count() query.
Reading the documentation of CActiveFinder, it looks that without togheter the query is done without the join, in order to select the main table and the related table in a separate statement.
That’s mean that without togheter you cannot put condition/group/having on fields that belongs to other tables.
So it looks that the SELECT is not bagous, it follows the documentation. What is strange is that the count is working properly, because, reading the documentation, it shouldn’t do the join in a single sql statement.
That’s fact is just an aspect of a strange behaviour of CActiveRecord::count() and CActiveRecord::find() follows different rules.
Because of that sometime happens that the count retrives wrong result (if there are HAVING clause, for example).
Do not rely on the fact that count and find and count are doing the same work, that’s not true.