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

Find using columns and hydration

I'm attempting to use the columns parameter to avoid selecting unnecessary data from my database. The columns selected could be none, a single column or anything inbetween depending on input parameters. I've overwritten the toArray method of my base class and extending classes and I have a problem when using the columns parameter in that object of my class types are no longer returned. I tried setting hydration to objects but it does nothing. I've read the documentation at https://docs.phalcon.io/en/latest/reference/models.html#finding-records and I understand why partial objects are returned. However, I don't understand why a partial record is returned when I explicitally set the hydration. This inconsistency can be corrected with the code below.

$robots = Robot::find(['columns' => 'id,name']);
$arrayOfRobots = [];
foreach($robots as $robot) {
    $robotData = $robot->toArray();
    $robot = new Robot();
    $robot->assign($robotData);
    $arrayOfRobots[] = $robot;
}

This workaround though must be applied conditionally depending on if the columns input exists.

Is there a way to force Phalcon to respect the hydration method and why does the hydration documentation not note that it is ignored when using the columns parameter?

Beacause when using columns parameter there are always returnd partial objects(or Row object or stdclass) beacause you are not selecting full object so why phalcon should return this as object ?

This is workaround, i just don't see any point in returning partial object. If you need full objects then select them.



1.1k

Full objects should be returned because I've explicitly set them to be returned as such.

$robots = Robots::find([
    'columns' => 'id, name',
    'hydration' => ResultSet::HYDRATE_RECORDS
]);

In this example hydration is completely ignored and the elements of the robots array are Row objects.

There are two separate issues here. The first is that hydration is ignored under certain circumstances. The second is that find and query can return objects of different types depending on non-obvious input.

The first issue I would consider a bug. If hydration is provided as an option, explicitly set and ignored the code is broken.

The second issue is more an issue of personal preference for the consistency in returned responses. I don't expect this to change as many before me have asked about what's going on when using the columns parameter and nothing has changed.

Beacause when using columns parameter there are always returnd partial objects(or Row object or stdclass) beacause you are not selecting full object so why phalcon should return this as object ?

This is workaround, i just don't see any point in returning partial object. If you need full objects then select them.

edited Apr '16

No. Full objects are only returned when selecting them as full object. If you select columns they will be always returned as partial.

It's good behaviour and it should stay as it is. There is no any issue. You just not selecting fll object - that's why you don't have returned your object, beacause YOU ARE NOT SELECTING FULL OBJECT.

Right now it's have the biggest consistency it can. You want objects - just select them. You are selecting columns ? You got partial objects.

Hydration as it documentation saying is for setting RESULTSET hydration, it's saying how RESULTSET will by hydrated. It's totally diffrent thing than object hydration. You get full objects only if you select all columns.