We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon Counting, Summing and Others not working through relationships

Hi.. I am having a problem with operations counting, summing or any other operation when I am doing the following command: $someModel->getFirst()->getTipo()->count();

or sum or anything It just bring me null ..

Other problem it is when I do directly SomeModel::count("some condition"); or It just doesn't work..

Does anyone had the same problem ?

Please am I doing something wrong ?

Many thanks..

Cheers!

When you call getTipo() you already execute the query so the count will be called on the resultset. I usually use the "pivotmodel" and call the count on that like

RobotPartPivotModel::count('robotID = 1');

As far as I am aware there is no way to count a relationship without quering the whole set and count localy in php.

Hi Varga..

So thanks for your answer.. but still.. when I do directly Model::count or Model::sum .. regardless that I declare some condition or not .. it returns null =(

Uhm You have to check your code, or show same sample because it should not return null in anycase, it can be 0 but not null, are the models works with Model::find()? It looks kinda suspicious at first glance.