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:
class User extends CI_Controller {
public function user($user_id) {
$this->load->model('user_model');
$data['username'] = $this->user_model->getName($user_id);
}
}
Model:
class User_model extends CI_Model {
public function getName($user_id) {
//Retrieve data from DB using the $user_id variable...
return $retrieved_data['name'];
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With