0

我是 MVC 和 Codeigniter 的新手,正在尝试让课程正常工作。

我正在运行 CodeIgniter 2.1.0 和 Doctrine 2.2.1

当我的代码调用 submit() 函数时,我得到Class 'Myclass_model' not found, 并引用包含以下内容的行:$u = new Myclass_model();

(见底部的编辑说明,现在进入Class 'Doctrine_Record' not found它扩展的模型)

在我的控制器中是以下代码:

public function submit() {

        if ($this->_submit_validate() === FALSE) {
            $this->index();
            return;    
        }

        $u = new Myclass_model();
        $u->username = $this->input->post('username');
        $u->password = $this->input->post('password');
        $u->email = $this->input->post('email');
        $u->save();

        $this->load->view('submit_success');
    }

在我的 /application/models/ 文件夹中,我有 myclass.php:

class Myclass extends Doctrine_Record {

public function setTableDefinition() {
    $this->hasColumn('username', 'string', 255, array('unique' => 'true'));
    $this->hasColumn('password', 'string', 255);
    $this->hasColumn('email', 'string', 255, array('unique' => 'true'));
}

public function setUp() {
    $this->setTableName('testtable1');
    $this->actAs('Timestampable');
            //$this->hasMutator('password', '_encrypt_password');
}

    protected function _encrypt_password($value) {
         $salt = '#*seCrEt!@-*%';
         $this->_set('password', md5($salt . $value));
         //Note: For mutators to work, auto_accessor_override option needs to be enabled. We have already done it, in our plugin file doctrine_pi.php.

    }
}

我怀疑我的问题在于扩展 Doctrine_Record。我安装了doctrine2,并且在/application/libraries/ 中有Doctrine.php 和Doctrine 文件夹。但我不确定在哪里检查,甚至不确定如何检查并确保 Doctrine_Record 可用、已配置等。

谁能帮我解决这个问题并找出问题所在?我的课简单吗?我的 Doctrine 安装/配置有问题吗?

编辑:我遵循了这样调用类的建议:

$this->load->model('Myclass_model','u');

现在我得到Class 'Doctrine_Record' not found了模型扩展 Doctrine_Record 的地方

4

1 回答 1

0

我怀疑您没有包含您的Myclass.php文件或路径不正确。如果您在使用 Doctrine 扩展时遇到问题,您的错误会提到父级。即便如此,请确保在新类中包含父文件include_once('path/to/doctrine_record');

您可以将其添加到autoload.php文件中或像这样手动添加:

public function submit() {

        if ($this->_submit_validate() === FALSE) {
            $this->index();
            return;    
        }

        include_once('/path/to/Myclass.php');
        $u = new Myclass();
        $u->username = $this->input->post('username');
        $u->password = $this->input->post('password');
        $u->email = $this->input->post('email');
        $u->save();

        $this->load->view('submit_success');
    }

或者更好的是,您可以像加载模型一样加载它。将文件重命名为myclass_model.php并将类名重命名为Myclass_model:)

public function submit() {

        if ($this->_submit_validate() === FALSE) {
            $this->index();
            return;    
        }

        $this->load->model('Myclass_model','u');
        $this->u->username = $this->input->post('username');
        $this->u->password = $this->input->post('password');
        $this->u->email = $this->input->post('email');
        $this->u->save();

        $this->load->view('submit_success');
    }
于 2012-04-04T19:15:28.653 回答