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

Resultset Simple toArray must rely on models in "collection" instead of DB

The resultSet Simple toArray does not executes the toArray method in each model and because of this, nor the jsonSerialize.

As I see it, Each model array should be returned using the toArray defined in it.

It is causing me a lot of problems with my API, since I must transverse each result to execute the jsonSerialize method by hand. Not so simple, since my jsonSerialize methods are fetching relationships and this relationships more relationships, each one with its own jsonSerialize.

Could be this be revised? Usually the "collections" are managed as "Composite pattern", so if i call toArray on a collection of items, it returns each item toArray. In this way, I can rely on fetching objects, even an objects with relationships, and getting my defined jsonSerializeor toArray of each one of them.

In pseudocode:

// each robot model on jsonSerialize returns some properties and the robot parts
// when calling json_encode on a Robot Model, it returns the Robot jsonSerialize, and the robot parts jsonSerialize
// in this way I can call

json_encode(Robots::find())

//and get a pretty json of robots, each one with its parts , serialized in the way "robot parts" decide.
edited Mar '17

I know that Resultsets are a transversable object with pointers to the database result, but...maybe we can find a way to abstract this one more layer? Would be great if we can override the "collection" object used for resultsets. So if I need it, I can override the resultset for "Robots" and encapsulate there the logic for my "Robot Collection". I know it is possible to do this overriding findFirstand find but it would be great if I don't need to override those on each model.

At least, most of the programmig code for web or apis are working over models, or model collections. In this way, I could rely on a better abstraction and separation of concerns.

edited Mar '17

From 3.1.0 there is option to use custom resultset classes for find methods, so you can override resultset toArray easly and do whatever you want there.

The phalcon aims for full performance, current implementation is as fast as it can be, hydrating every record in resultset to model and calling model to Array in resultset toArray method will be much much slower.

Great! To be able to override Resultsets is a great addition! I understand about the hydrate problem on toArray, but I wonder if some kind of switch for this behaviour would be great. Don't know the use cases for most ppl but actually, for this API use case I'm doing the foreach over the resultset in each scenario. Now with the override on the resultset I just need to create a Resultset subclass without the need of replacing the original ResultSet. Thanks.

From 3.1.0 there is option to use custom resultset classes for find methods, so you can override resultset toArray easly and do whatever you want there.

The phalcon aims for full performance, current implementation is as fast as it can be, hydrating every record in resultset to model and calling model to Array in resultset toArray method will be much much slower.