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

Model resultset find and specific operations

Hi, As per docs https://docs.phalcon.io/en/3.3/db-models

the find() method returns a Phalcon\Mvc\Model\Resultset\Simple. This is an object that encapsulates all the functionality a resultset has like traversing, seeking specific records, counting, etc.

Say I have a "units" table with type as big , small, large.

I can do

      $result = Units::find():
      $totalRecords = $result->count();

However, is it possible to find total on the basis of type ie. how many large , small and big units are in there from resultset itself and without making another query to DB ?

Hi @iRickyj is it the same problem as before?


Not really, actually real problem is that I have worked a lot with procedural programming but never with any framework and OOP, so here my doubt is more about resultset object created using find().

When we do this on resultset : $totalRecords = $result->count();

Are we using new sql like SELECT count("type") FROM "units"; or its couting number of records from Object itself ?

Similarly, once we have a resultset , can we do things similar to : SELECT count("type") FROM units WHERE type = 'large'; but without actually connecting to DB as we already have all data from MODEL::find() ?


unless so

$UserSession = UserSession::query() ->columns([ "COUNT() AS count_total", "(SELECT COUNT() FROM UserSession WHERE compromised = TRUE) AS count_compromised", "(SELECT COUNT(*) FROM UserSession WHERE active = TRUE) AS count_active", ]) ->cache([ "key" => "sessions_info" ]) ->execute() ->getFirst() ->toArray();

to go through everything in memory is resource intensive


Thanks but I am still naive in all this, can you explain what you are try to say ?


This question is not yet answered, may be I can explain further: To make it simple, I have a large table with many thousands of record, the records are divided into type : "important", "urgent", "regular". Now I need to find total number of "important" records, "urgent" records and "regular" records.

What will be the most efficient way to do it ?