Class Model not found in Doctrine 2.2 + CodeIgnite

2019-08-04 05:48发布

问题:

I have set up a project with CodeIgniter 2.1 and Doctrine 2.2, following the instruction on Doctrine cookbook. The EntityManager works, but when I try to load entity models, it gives me error

Fatal error: Class 'Users' not found in /Volumes/Data/Projects/myproject/application/controllers/home.php on line 10 

This is my home.php file:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

//require_once(APPPATH.'models/Users.php');

class Home extends CI_Controller {
  public function index()
  {
    $em = $this->doctrine->em;
    $users = new Users;     
    //$user = $em->find("Users", 1);
    $em->flush(); // dummy
    $this->load->view('welcome_message');
  }
}

If I uncomment line 3: require_once(APPPATH.'models/Users.php');, then it works perfectly.

How can I make the models auto-loaded?

Is the autoload mechanism handled by the bootstrap in libraries/ Doctrine.php, isn't it?

    $entitiesClassLoader = new ClassLoader('models', rtrim(APPPATH, "/" )); 
    $entitiesClassLoader->register(); 

Please anyone give me insight about this issue.

回答1:

I'm guessing that you have a namespace line inside of your Users.php file?

If so then you will need to add a "use" statement to your controller code so the php namespace system knows where to look for your entities.

If not then verify that your entitiesClassLoader is actually being called and that APPPATH has the expected value.



回答2:

I've never worked with CI and Doctrine so don't know if Doctrine should load the models or where it loads them, but you should try to do it yourselft, not with an include but this way :

In the Controller : $this->load->model('Users');

Or as Autoload in application/config/autoload.php and load the model in the Array.