简体   繁体   中英

Class Model not found in Doctrine 2.2 + CodeIgniter 2.1

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM