It seems that it is impossible to pass a variable to a Model’s Constructor in Code Igniter. Maybe I’m missing something in MVC or Code Igniter, but I don’t understand what.
In my case, should I use librairies instead of Model ? (librairies actually accept parameters)
Actually my code is looking like this, and I think it’s a bit strange :
Controller :
class User extends CI_Controller {
public function user($user_id) {
$this->load->model('user');
$this->user->setId($user_id);
$data['username'] = $this->user->getName();
// Isn't it a bit strange ? I can load the class User even if the User doesn't mean anything ?
}
}
Model :
class User extends CI_Model {
private $id;
private $name;
public function setId($id) {
$this->id = $id;
// Retrieve data from DB...
$this->name = $retrieved_data['name'];
}
public function getName() {
return $this->name;
}
}
Try this:
Controller:
Model:
Seems like a more logical and faster way to do it. Not sure if it complies with the rest of your code, but it might help you track down the errors.