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.

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?



1.5k
Accepted
answer

SOLVED

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();