yii\db\Query sum return 0 if null

I wonder if I m missing something about sum function ( http://www.yiiframework.com/doc-2.0/yii-db-query.html#sum()-detail )

If the result from sum is null, I assign 0 to it as demonstrate below




$totalValue = Book::find()->where(['type' => 'Fiction'])->sum('[[value]]');


$totalValue = ($totalValue == null) ? 0 : $totalValue;



Is there a way to built in the default to 0 if null within sum() ?

$totalValue = Book::find()->where([‘type’ => ‘Fiction’])->sum(‘case when value=’’ then 1 else 0 end’);

your code doesnt do. I want if the sum is null, make it 0, else, return the sum.

How about


$totalValue = (int) Book::find()->where(['type' => 'Fiction'])->sum('[[value]]');

that works. thx