skip to Main Content

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


  1. 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?

    Login or Signup to reply.
  2. if other_model has relation with Zone (defined in model) then :

    Zone->other_model->field;

    else in head of file

    "use PhalconMvcModel*other_model*;

    Login or Signup to reply.
  3. Try to define the namespace in your model, like:

    // app/models/Zone.php
    
    <?php
    
    namespace MyAppModels;
    
    use PhalconMvcModel;
    
    class Zone extends Model
    {
         public function initialize()
         {
              $this->setSource('zone');
         }
     }
    

    Then in your loader:

    <?php
    
    use PhalconLoader;
    
    $loader->registerNamespaces(
       [
          'MyAppModels' => 'app/models'
       ]
    );
    
    $loader->register();
    

    Then in your controller:

    <?php
    
    use MyAppModelsZone;
    

    Your current indexAction should work, but beware of what I commented to you regarding fields with underscores:

    $zone = Zone::findFirstByid_zone($id_zone); //won't work
    
    $zone = Zone::findFirstByIdZone($id_zone);  //works
    

    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 after zone is working.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search