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

Dynamic Models in Base Controller

Trying to implement a basic CRUD controller.

In the parent controllers I define which model will be used for that specific controller. In the base class I then want to be able to say something like:

$model = $this->getModelName(); //This function is defined in every controller
$model = new $model;

Now I cant do the new $model due to it not being loaded in the namespace. What is the best way of going about that? I dont really want to go down the route of having a 20 line use statement at the top of the controller or is that the only option?



Pretty simple actually but not obvious straight away as my models are outside of the normal phalcon app structure. I just predefine the namespace and it works as expected.

$model = '\\models\\' . $this->getModelName();
$model = new $model();