问题
I'm new to MVC and Codeigniter and am trying to get a class working.
I'm running CodeIgniter 2.1.0 and Doctrine 2.2.1
When my code calls the submit() function, I get Class 'Myclass_model' not found
, and references to the line that includes: $u = new Myclass_model();
(See edit note at the bottom, now getting Class 'Doctrine_Record' not found
in the model where it is extended)
In my controller is the following code:
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');
}
And in my /application/models/ folder I have 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.
}
}
I suspect that my problem is with extending Doctrine_Record. I have doctrine2 installed, and in /application/libraries/ I have Doctrine.php and the Doctrine folder. But I'm not sure where to check, or even how to check and make sure that Doctrine_Record is available, configured, etc.
Can anyone help me troubleshoot this and figure out where the problem lies? Something simple with my class? Some problem with my Doctrine install/config?
Edit: I followed the suggestion of calling the class like so:
$this->load->model('Myclass_model','u');
And am now getting Class 'Doctrine_Record' not found
where the model extends Doctrine_Record
回答1:
I suspect you didn't include your Myclass.php
file or have an incorrect path. If you were having issues with the Doctrine extension, your error would mention the parent. Even still, make sure you include the parent file inside your new class with a include_once('path/to/doctrine_record');
You can add it to the autoload.php
file or manually like so :
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');
}
Or even better, you load it like a model. Rename the file to myclass_model.php
and the class name to 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');
}
来源:https://stackoverflow.com/questions/10017730/class-not-found