Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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?



1.7k
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).