代码之家  ›  专栏  ›  技术社区  ›  TCCV

不能用条令在CodeIgniter中加载模型

  •  3
  • TCCV  · 技术社区  · 14 年前

    我一直在试用CodeIgniter。到目前为止,我喜欢。我决定也尝试一下理论,因为我听说了很多关于ORM的东西,而且这个理论很好。我找到了一些教程:

    我已经尝试了所有的方法,我在那些尝试建立一个工作条令安装,但我仍然无法得到我的模型加载。所有这些文档中的一个共同点是,它们都是旧版本,引用了document和/或CI的旧版本,甚至连doctine自己的文档都引用了0.11版本。我使用的是CodeIgniter-1.7.2和document-1.2.3。

    有人有最新的文件吗?有人能看出下面的代码哪里出错了吗?这是基于phpandstuff链接。

    File Structure
    root
       \_system
            \_application
                \_config
                \_controllers
                \_models
                \_views
            \_plugins
                \_doctrine
                    \_lib
                        \_Doctrine.php
                \_doctrine_pi.php
    

    系统/插件/条令_圆周率.php

    // load Doctrine library
    require_once BASEPATH.'/plugins/doctrine/lib/Doctrine.php';
    
    // load database configuration from CodeIgniter
    require_once APPPATH.'/config/database.php';
    
    // this will allow Doctrine to load Model classes automatically
    
    //below is the suggested method of loading models
    //I also tried a user's suggestion of "modelsAutoload"
    //spl_autoload_register(array('Doctrine', 'autoload'));
    //spl_autoload_register(array('Doctrine_Core', 'modelsAutoload'));
    
    // we load our database connections into Doctrine_Manager
    // this loop allows us to use multiple connections later on
    foreach ($db as $connection_name => $db_values) {
    
        // first we must convert to dsn format
        $dsn = $db[$connection_name]['dbdriver'] .
            '://' . $db[$connection_name]['username'] .
            ':' . $db[$connection_name]['password'].
            '@' . $db[$connection_name]['hostname'] .
            '/' . $db[$connection_name]['database'];
    
        Doctrine_Manager::connection($dsn,$connection_name);
    }
    
    // CodeIgniter's Model class needs to be loaded
    require_once BASEPATH.'/libraries/Model.php';
    
    // telling Doctrine where our models are located
    //Below is another user's suggestion, this also fails
    //within my setup.
    spl_autoload_register(array('Doctrine', 'autoload'));
    spl_autoload_register(array('Doctrine', 'modelsAutoload'));
    $manager = Doctrine_Manager::getInstance();
    
    $manager->setAttribute(Doctrine::ATTR_MODEL_LOADING, Doctrine::MODEL_LOADING_CONSERVATIVE);
    $manager->setAttribute(Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES, true);
    $manager->setAttribute(Doctrine_Core::ATTR_AUTOLOAD_TABLE_CLASSES, true);
    Doctrine::loadModels(APPPATH.'models');
    
    
    // (OPTIONAL) CONFIGURATION BELOW
    
    // this will allow us to use "mutators"
    Doctrine_Manager::getInstance()->setAttribute(
        Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
    
    // this sets all table columns to notnull and unsigned (for ints) by default
    Doctrine_Manager::getInstance()->setAttribute(
        Doctrine::ATTR_DEFAULT_COLUMN_OPTIONS,
        array('notnull' => true, 'unsigned' => true));
    
    // set the default primary key to be named 'id', integer, 4 bytes
    Doctrine_Manager::getInstance()->setAttribute(
        Doctrine::ATTR_DEFAULT_IDENTIFIER_OPTIONS,
        array('name' => 'id', 'type' => 'integer', 'length' => 4));
    

    从system/application/config/自动加载.php

    /*
    | -------------------------------------------------------------------
    |  Auto-load Plugins
    | -------------------------------------------------------------------
    | Prototype:
    |
    |   $autoload['plugin'] = array('captcha', 'js_calendar');
    */
    
    $autoload['plugin'] = array('doctrine');
    

    class User extends Doctrine_Record {
    
        function __construct(){
            parent::__construct();
        }
    
        function setTableDefinition(){
            $this->hasDefinition("username", "string", 255);
            $this->hasDefinition("password", "string", 255);
        }
    
    }
    

    如果有人想见我的管制员或其他什么,请告诉我。 控制器只是 echo

    2 回复  |  直到 14 年前
        1
  •  1
  •   onktak    14 年前

    我遵循了这里的教程: http://www.scribd.com/Ci-Doctrine/d/43303007

    应用程序路径 可能设置得不好,例如您可能会得到类似C:\wamp\www\v1/system/application/之类的东西。所以把你的 $system\u文件夹 索引.php 在根目录上。

    享受

        2
  •  0
  •   Erik    14 年前

    这就是我的设置。这是一个zendframework应用程序,但是你应该能够毫无问题地将其转换为CI。

    try {
        Doctrine_Manager::getInstance()->setAttribute(Doctrine::ATTR_MODEL_LOADING, Doctrine::MODEL_LOADING_AGGRESSIVE);
        Doctrine_Manager::getInstance()->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_ALL);
        $conn = Doctrine_Manager::connection($this->_db->getConnection());
        Doctrine::loadModels($this->_root . '/application/models');
    } catch (Zend_Db_Adapter_Exception $e) {
        echo 'Incorrect database login: ' . $e->getMessage();
    }
    

    我还要检查一下你提到的模型装载位置是否正确。这类问题通常是由于错误地引用了您的条令库或模型所在的位置。

    推荐文章