Welcome Guest, Not a member yet? Register   Sign In
Calling function in superclass but using model in current class
#1

[eluser]Mr.Data[/eluser]
Hello!
I have a function which is used in several classes. So my idea is to put this function into the upper class MY_Controller. Now it looks something like this
Code:
class Super_Controller extends CI_Controller {
    public function doSomething($target) {
        ...
        $this->model->update($target, "id = $id");
        ...
    }
}

class SomeClass extends Super_Controller {
    public function doSomething() {
        parent::doSomething($target);
    }
}
Unfortunately this does not work because the superclass apparently doesn't know anything about the model. Is there a possibility to get this working?
#2

[eluser]JoostV[/eluser]
Simply load the model inside your super class.
Code:
class Super_Controller extends CI_Controller {
    public function doSomething($target) {
        $this->load->model('some_model');
        $this->some_model->update($target, "id = $id");
    }
}

By the way, you might like the MY_Controller video tutorial at http://codeigniter.tv/a-10/Extending-the...and-beyond
#3

[eluser]Mr.Data[/eluser]
The used model is dependent on the subclass. OK, but I could pass it as a parameter. But I am also using several calls to others functions. Look at this:
Code:
class Super_Controller extends CI_Controller {
    public function doSomething($target) {
        if ($this->input->post('submit')) {
            ....
            $this->model->update($target, "id = $id");
            ...
        }
    }
}
I don't know if the upper class has access to the POST array of the subclass. Is it possible to pass the whole reference of the subclass to the super class? It would look like this
Code:
parent::doSomething($target, &$this);
#4

[eluser]JoostV[/eluser]
$this->input->post('submit') is available in all controllers. No need to pass $this.
#5

[eluser]Mr.Data[/eluser]
OK, passing the model to the superclass was the solution to this problem. Now everything is fine.




Theme © iAndrew 2016 - Forum software by © MyBB