HMVC架构CodeIgniter3中CI_Doctrine未找到问题技术求助
Hey there, let's work through that CI_Doctrine not found error you're facing in your HMVC CodeIgniter 3 setup. Based on your code and setup details, here are the most likely fixes to get things running smoothly:
1. Verify the CI_Doctrine Library Exists & Is Properly Configured
First off, make sure you have a valid CI_Doctrine.php file in your application/libraries directory. This class is responsible for bootstrapping Doctrine's Entity Manager, and if it's missing or misnamed, you'll hit this error right away. A basic, functional version looks like this:
class CI_Doctrine { public $em; public function __construct() { // Load Doctrine's autoloader (adjust path if you used Composer) require_once APPPATH . 'third_party/Doctrine/ORM/Tools/Setup.php'; \Doctrine\ORM\Tools\Setup::registerAutoloadGit(); // Configure metadata settings (point to your entity files) $isDevMode = ENVIRONMENT === 'development'; $config = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration( [APPPATH . 'models/entities'], $isDevMode ); // Reuse your CI database config to avoid duplication $connectionParams = [ 'driver' => 'pdo_mysql', 'user' => $this->db->username, 'password' => $this->db->password, 'dbname' => $this->db->database, 'host' => $this->db->hostname, ]; // Initialize the Entity Manager $this->em = \Doctrine\ORM\EntityManager::create($connectionParams, $config); } }
2. Fix the Controller Inheritance Gap
Your MY_Controller inherits MX_Controller and likely handles Doctrine initialization, but your Orm class extends CI_Controller directly—meaning it doesn't get access to the pre-loaded Doctrine instance. You have two straightforward fixes here:
Option A: Switch Orm to Extend MY_Controller
Since your MY_Controller already has HMVC support and (presumably) Doctrine set up, changing the class inheritance will give your Orm class instant access to $this->doctrine:
class Orm extends MY_Controller { // Changed from CI_Controller private $st; private $cmf; public function __construct() { parent::__construct(); // Now $this->doctrine is available $this->st = new \Doctrine\ORM\Tools\SchemaTool($this->doctrine->em); $this->cmf = $this->doctrine->em->getMetadataFactory(); $this->cmf->setEntityManager($this->doctrine->em); // ... rest of your code } }
Option B: Manually Load Doctrine in Orm's Constructor
If you need Orm to stay a direct CI_Controller subclass, explicitly load the Doctrine library in its constructor:
public function __construct() { parent::__construct(); $this->load->library('doctrine'); // Load the CI_Doctrine library manually // Now you can access $this->doctrine as expected $this->st = new \Doctrine\ORM\Tools\SchemaTool($this->doctrine->em); $this->cmf = $this->doctrine->em->getMetadataFactory(); $this->cmf->setEntityManager($this->doctrine->em); // ... rest of your code }
3. Check Composer Autoload (If You Installed Doctrine via Composer)
If you used Composer to install Doctrine, make sure CodeIgniter is using the Composer autoloader. Open application/config/config.php and set:
$config['composer_autoload'] = TRUE;
This ensures Doctrine's core classes are automatically available without manual require statements.
4. Confirm HMVC Module Loading Order
In HMVC setups, module load order can sometimes affect library availability. Check your application/config/modules.php to ensure your orm module is loaded after any core setup that initializes Doctrine. Alternatively, add Doctrine to the module's own autoload config if your HMVC setup supports that.
内容的提问来源于stack exchange,提问作者surma

