How to call a model in another controller?
I explain myself, I created a controller and I try to call another model in this controller, but I have the error "Error: Class not found".
You can see the code of my controller "Adserver" trying to call the model "Zone"
<?php
declare(strict_types=1);
use PhalconMvcModelZone as Zone;
class AdserveurController extends ControllerBase
{
public function indexAction()
{
$id_zone= 1;
$zone = Zone::findFirstByid_zone($id_zone);
if(!zone){
$this->flashSession->error('erreur id');
return $this->response->redirect();
}
print_r ($id_zone);
$zone = Zone::findFirst($hauteur);
if(!zone){
$this->fashSession->error('erreur hauteur');
return $this->response->redirect();
}
$zone = Zone::findFirst($largeur);
if(!zone){
$this->fashSession->error('erreur hauteur');
return $this->response->redirect();
}
}
}
On top of my controller, I tried the "use PhalconMvcModel" and the error persists.
My phalco version is 4.0
Could someone help me on how to call two models in a separate controller?
Thanks.
3
Answers
I call multiple models from withing various controllers all the time and don’t have to declare "use PhalconMvcModelZone as Zone;" at the top.
How is your Zone model file defined?
if other_model has relation with Zone (defined in model) then :
Zone->other_model->field;
else in head of file
"use PhalconMvcModel*other_model*;
Try to define the namespace in your model, like:
Then in your loader:
Then in your controller:
Your current
indexAction
should work, but beware of what I commented to you regarding fields with underscores:Since you commented that you are a beginner, I would recommend you to review the MVC examples for Phalcon projects: https://github.com/phalcon/mvc So you get acquainted with namespaces in your app.
Also, if you are working on a large application, it’s better that you register namespaces than directories for performance reasons, as detailed in the Docs: https://docs.phalcon.io/4.0/en/loader
Ps. Reviewing
indexAction
there are some parameters that are not defined, like$hauteur
and$largeur
. Since$zone
still is not working, these issues are still not visible –but they’ll show up afterzone
is working.