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 on a model obtain with call_user_func

Hi,

I use to call my data in a specific situation like that:

call_user_func(array($tableName, 'find'), array(

    'conditions' => $parameter,
));

Is it possible that afterFetch is not call in this situation ?

Thank you.

If you will select not full models then yes. Also don't use call_user_func, just use $tableName::find();



31.3k

I select the whole models. The function still not firing. I don't get it. I made the change mproposed by Wojciech. But, same problem.

The call :

$data = $tableName::find(array(

    'conditions' => $parameter,
    'order' => $tableDefinitionColumn->ColumnName .  " $direction"
));

The model :

public function initialize() {

    $this->setSource('city_names');
}

public function columnMap() {

    return [

        "id_city" => "ID",
        "country_name" => "CountryName",
        "city_name_ok" => "CityNameCorrected",
        "to_skip" => "ToSkip",
        "is_deleted" => "IsDeleted",
        "province_name" => "StateName",
        "city_name" => "CityName",
        "time" => "Time",
        "is_google_checked" => "IsGoogleChecked",
    ];
}

public function afterFetch() {

    $this->Time = date_format('Y-m-d', $this->Time);
}
edited Mar '17

Oh i just didnt get your question :C Yes, i thought you are looking way TO NOT FIRE AFTER FETCH. AfterFetch will be fired when you will access any model from resultset. It's not fired on ::find alone, model instance is created once you will access any row, toArray method is returning resultset from database raw data + columnMap - this is making resultset really fast.



31.3k

OK, so if I understand correctly, when I execute

$data->toArray();

afterFetch should be executed ? Because he is not. The other way I understand it is that when I loop over the dataset like :

foreach($data as $d) {

    echo $d->CityName;
}

on each iterations, the afterFetch is executed ?

Thank you.

edited Mar '17

@Jurigag I have read in forum a debate over the ways to query join data from multiple tables, and I choose to use a custom query (QueryBuilder) to fetch the data doing all the joins in a single request. BUT this way i have observed that the result set is a "generic" dataset since I'm not using the raw model (maybe im a dumb programmer and coder, possibly), and as consequence afterFetch will never be executed it seems.

There is another way to format the result of the query?

I'm trying to do the same as Daniel, just format the date. The date is just a read field, since the database timestamps when the data is created/updated. Since is ungratful to change data from the resultset object, I'm thinking about to use a raw sql query, but then whats the point again? lol ^^

Thanks for reading this and any help that could be given, I'm much appreciated!

$query = new Phalcon\Mvc\Model\Query\Builder();

$query->addFrom("Shroubles")
->columns(array('several.columns')
->join('Shrent','Shroubles.id = Shrent.id')
->join('Shrongas','Shroubles.shorongaId = Shorongas.id')
->where('condition');

$result = $query->getQuery()-execute();

P.S.: I know it's not good, but it was the best i could do at the moment. P.P.S.: Sorry for my bad english, non-native speaker here.

You can always format it yourself. Not sure if this new thing from 3.1.0 will work on not full models, you would need to check. If you don't select full models then result will be Phalcon\Mvc\Model\Row, not your Model class causing it to not execut afterFetch, which is good thing, because you don't have full model and all columns and in this afterFetch method you could change some not existing columns for example.