REST API and aggregate queries in related record


(Ziggi) #1

Hi,

For whatever reason I am unable to API to return aggregates from related record.

For instance I have a person who has (many) orders and each order has (many) items. I need to show a total for each order, so I write:

$model = Person::find()->with(['orders' => function($q) {
    $q->joinWith(['items' => function($q) {
        $q->select('amount')->sum();
    }]);
}])->where(['person.id' => $pid])->one();

$model->toArray([], ['orders.items]);

But this does not work!

Any idea what is the right way (besides using a database view as a relational model)?

Thanks ahead for any clue!