Ran Into A Big Limitation Of Self::stat

While playing around with stat relations, I ran into an issue where it was just plain not working. no matter what, it was returning 0.

During tracing, I had found that the Gii model generator had identified a numeric column (unsigned bigint(10)) as a string which was noted in several threads that was done to prevent loss of precision.

For some reason, it just will not sum, count, avg, or what have you.

I can run a custom query but the relation just won’t handle it properly.

Any suggestions besides going to a raw query or using the query builder? I am thoroughly enjoying the ActiveRecord model but this is a pretty glaring problem.

My experience with self::stat is very limited, but I believe that whichever additional option you provide in the definition of relations() will be used build a custom query behind the scenes.

So, this piece of code:

public function relations()


	return array(

		'total'=>array(self::STAT, 'Table', 'ForeignKey', 'select'=>'SUM(`unitPrice`)'),



might produce something like this:

SELECT SUM(`unitPrice`) as `total` FROM CURRENT_TABLE JOIN `Table` ON ...

and you won’t be able to sum the string unitPrice.

My recommendation is to leave your table column as a signed integer. Whenever I’ve tried to save that one bit of space, I’ve run into a similar issue and CActiveRecord starts complaining one way or another.

Hope this helps!

Thanks for the tidbit. I assumed that it would just construct a query however no matter what, it always returns 0. If I use a raw query, it responds with the expected value.

That’s just odd!