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

afterFetch - columns

If i have in my model:

public function afterFetch(){
      $this->user_postcode_range = $this->user_postcode_range != '' ? unserialize($this->user_postcode_range) : array();
}

When i use this find, my user_postcode_range is not unserialized

$user = Users::findFirst(
    array(
        "columns" => array("user_postcode_range"),
        "conditions" => "user_active = 1"
    )
);

When i use this, my user_postcode_range is unserialized. Why when i use columns in my query my model don't unserialize data ?:

$user = Users::findFirst(
    array(
        "conditions" => "user_active = 1"
    )
);


2.1k
edited Feb '15

this is interesting. they should both trigger afterFetch. let me trace it =d

my best guess is maybe it returns a simple/complex type, all query and codes are the same, but the result type is different.

let value = Model::cloneResultMap(column["instance"], rowModel, columnMap, dirtyState, keepSnapshots); //complex
let activeRow = Model::cloneResultMap( //simple
                this->_model,
                row,
                columnMap,
                Model::DIRTY_STATE_PERSISTENT,
                this->_keepSnapshots
            );
edited Feb '15

7thcubic is right. When you specify the 'columns', Phalcon returns a standard object with the specified columns as properties (so not the full model). If you don't specify the columns, Phalcon retrieves all of them and returns the actual model to you.

The reason is that Phalcon can't build a full model if you only retrieve certain columns.

The difference in work the server has to do between retrieving 1 column or all columns is almost always negligable. I wouldn't bother specifying the column.