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

Access config variables inside the model

I want to write some data to a file whenever my model is saved, so I added the appropriate code to do this in the beforeSave method in my model. However, the path to save to is constructed using values from the config loaded into the di. In a controller this is accessible as $this->config->configVar but $this->config is not valid in the model (undefined property).

What is the correct way to access components setup in the di from the model?



3.9k
Accepted
answer

At the bottom of the Models reference under the Injecting services into Models I found

$this->getDI()->getFlash();

By replacing it with

$this->getDI()->getConfig();

I can access the config component of the di.

Does that apply to Phalcon 3.0.1?

Slightly different (the above may still work but it may have been removed/deprecated). Use this instead

$this->getDI()->get("config")

This is used in Vokuro example (my addition to allow easy disabling of emails).