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

Phalcon virtual field which is not defined in map

Hello everyone,

I've read carefully information about virtual fields on this forum. What do I need:

I have some translations for model in separated table. I would like to attach them as a virtual field via trait to the model.

Example: Table & Model: category ($id, $systemName) Table: category_translation ($parentId related to category.id, $locale, $name)

So category hasMany category_translation records (1 row per 1 locale). So after some manipulations, I have an array

$locales = [ 'en' => 'value', 'tr' => 'other_value', 'es' => 'es value', ... ];

Now I would like to attach this array to category model on fetch.

public function afterFetch() { $this->locales = $this->methodThatReturnsAnArray(); }

But it doesn't work, because locales column is not exists in table. Option is to create an empty unused field in table, but this is definitely not a right way.

What is a best option to add a virtual unexisting in table column to model mapping?

Would not you be calculating the same thing many times? I mean, why do not you just add this method to your model?