Class Model not found in Doctrine 2.2 + CodeIgniter 2.1

浪尽此生 提交于 2019-12-13 00:41:14

问题


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.



来源:https://stackoverflow.com/questions/9396796/class-model-not-found-in-doctrine-2-2-codeigniter-2-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!