findAll() and count() do different things with same criteria

So I have this action happening:



		$joins = array(


				'sampleRequiredMeasurement' =>


						array(


							'select' => 'sampleMeasurementTypeId',


							'condition'=>'"sampleMeasurementTypeId" IS NULL',


							),


				'property', 'sampleType', 'sampleStatus'


				);


		


		$pages=new CPagination(sample::model()->with( $joins )->count($criteria));


		$pages->pageSize=self::PAGE_SIZE;


		$pages->applyLimit($criteria);








		$sort=new CSort('sample');


		$sort->applyOrder($criteria);





		$sampleList=sample::model()->with( $joins )->findAll($criteria);


And yet I do not get the result I would expect. The count() forms the query I expect, but the findAll does two queries and as a result I get a resultSet with more rows than count() expects.

From the actual log file:



2009/04/01 15:16:11 [trace] [system.db.CDbCommand] query with SQL: SELECT COUNT(DISTINCT "sample"."id") FROM "sample"  LEFT OUTER JOIN "sampleRequiredMeasurement" t1 ON t1."sampleId"="sample"."id" LEFT OUTER JOIN "property" t2 ON "sample"."propertyId"=t2."id" LEFT OUTER JOIN "sampleType" t3 ON "sample"."sampleTypeId"=t3."id" LEFT OUTER JOIN "sampleStatus" t4 ON "sample"."sampleStatusId"=t4."id" WHERE "sampleStatusId"=:sampleStatusId AND "sampleMeasurementTypeId" IS NULL





2009/04/01 15:16:11 [trace] [system.db.CDbCommand] query with SQL: SELECT "sample"."id" AS t0_c0, "sample"."identifier" AS t0_c1, "sample"."propertyId" AS t0_c2, "sample"."latitude" AS t0_c3, "sample"."longitude" AS t0_c4, "sample"."siteIdentifier" AS t0_c5, "sample"."depthInInches" AS t0_c6, "sample"."sampleTypeId" AS t0_c7, "sample"."dateSampleAcquired" AS t0_c8, "sample"."gridMapURL" AS t0_c9, "sample"."sampleStatusId" AS t0_c10, "sample"."netWeight" AS t0_c11, "sample"."netWeightUnitOfMeasureId" AS t0_c12, t2."id" AS t2_c0, t2."property" AS t2_c1, t2."projectId" AS t2_c2, t3."id" AS t3_c0, t3."sampleType" AS t3_c1, t4."id" AS t4_c0, t4."sampleStatus" AS t4_c1 FROM "sample"  LEFT OUTER JOIN "property" t2 ON "sample"."propertyId"=t2."id" LEFT OUTER JOIN "sampleType" t3 ON "sample"."sampleTypeId"=t3."id" LEFT OUTER JOIN "sampleStatus" t4 ON "sample"."sampleStatusId"=t4."id" WHERE "sampleStatusId"=:sampleStatusId LIMIT 10





2009/04/01 15:16:11 [trace] [system.db.CDbCommand] query with SQL: SELECT "sample"."id" AS t0_c0, t1."sampleMeasurementTypeId" AS t1_c2, t1."id" AS t1_c0 FROM "sample" LEFT OUTER JOIN "sampleRequiredMeasurement" t1 ON t1."sampleId"="sample"."id" WHERE "sample"."id" IN (4, 12, 5) AND "sampleMeasurementTypeId" IS NULL





What I expected to see in the log file:






2009/04/01 15:16:11 [trace] [system.db.CDbCommand] query with SQL: SELECT COUNT(DISTINCT "sample"."id") FROM "sample"  LEFT OUTER JOIN "sampleRequiredMeasurement" t1 ON t1."sampleId"="sample"."id" LEFT OUTER JOIN "property" t2 ON "sample"."propertyId"=t2."id" LEFT OUTER JOIN "sampleType" t3 ON "sample"."sampleTypeId"=t3."id" LEFT OUTER JOIN "sampleStatus" t4 ON "sample"."sampleStatusId"=t4."id" WHERE "sampleStatusId"=:sampleStatusId AND "sampleMeasurementTypeId" IS NULL





2009/04/01 15:16:11 [trace] [system.db.CDbCommand] query with SQL: SELECT "sample"."id" AS t0_c0, "sample"."identifier" AS t0_c1, "sample"."propertyId" AS t0_c2, "sample"."latitude" AS t0_c3, "sample"."longitude" AS t0_c4, "sample"."siteIdentifier" AS t0_c5, "sample"."depthInInches" AS t0_c6, "sample"."sampleTypeId" AS t0_c7, "sample"."dateSampleAcquired" AS t0_c8, "sample"."gridMapURL" AS t0_c9, "sample"."sampleStatusId" AS t0_c10, "sample"."netWeight" AS t0_c11, "sample"."netWeightUnitOfMeasureId" AS t0_c12, t1."sampleMeasurementTypeId" AS t1_c2,  t2."id" AS t2_c0, t2."property" AS t2_c1, t2."projectId" AS t2_c2, t3."id" AS t3_c0, t3."sampleType" AS t3_c1, t4."id" AS t4_c0, t4."sampleStatus" AS t4_c1 FROM "sample" LEFT OUTER JOIN "sampleRequiredMeasurement" t1 ON t1."sampleId"="sample"."id" LEFT OUTER JOIN "property" t2 ON "sample"."propertyId"=t2."id" LEFT OUTER JOIN "sampleType" t3 ON "sample"."sampleTypeId"=t3."id" LEFT OUTER JOIN "sampleStatus" t4 ON "sample"."sampleStatusId"=t4."id" WHERE "sampleStatusId"=:sampleStatusId  AND "sampleMeasurementTypeId" IS NULL LIMIT 10





It seems wierd to me that the second query in the log file even leaves t1 unspecified and then runs a left join on the resultset, but does not take out the results that don't match. I feel like I must be using something improperly since it seems like yii is trying to figure out what I mean. What am I doing incorrectly?

Use ->with(…)->together()->findAll()

However, you need to make sure this gives back correct data.

qiang the magic man fixes all problems  8)

Thanks again!