Welcome Guest, Not a member yet? Register   Sign In
Acces controller function from library / alternative work around?
#1

[eluser]Bramme[/eluser]
Hey everybody

Here's my issue: I'm developping a CRUD application that generates standard HTML tables and forms for my database tables. I've extended the CI_Controller with MY_Controller and have written a few basic methods (index(), edit()...) that generate default html. For every table in my database, I then create a controller that extends from MY_Controller. This ensures every controller, extended from MY_Controller has the same pages (index, edit).

If I need to display something other than the default HTML, I just overwrite the index/edit function in my table controller.

The index page of MY_Controller produces an HTML table with all the fields in the database and displays those fields as flat text. The function that generates the html table, is in a library called Crud. So in crud.php I do this at a certain point:
Code:
foreach ($query->result() as $temp_row)
{
    foreach ($fields as $field => $field_options)
    {            
        $row[] = $temp_row->$field;
    }
    $this->CI->table->add_row($row);
}
Now, sometimes, I want something more than flat text. So I though I would try something as
Code:
foreach ($query->result() as $temp_row)
{
    foreach ($fields as $field => $field_options)
    {            
        $row[] = $this->CI->MY_Controller->_format_table_field($field, $temp_row);
    }
    $this->CI->table->add_row($row);
}
And put the following in MY_Controller.php
Code:
function _format_table_field($field, $datarow)
{
    return $datarow->$field;
}
Do I need something more fancy, then I just overwrite _format_table_field in table_controller.php. However, this does not work, giving me a "Fatal error: Call to a member function _format_table_field() on a non-object" (I tried CI_Controller->_format_table_field and Controller->_format_table_field too).

Does anybody have a suggestion as to how to solve this?
#2

[eluser]Adam K[/eluser]
Hi Bramme,

as I mentioned on twitter, I'm not sure about three things:

- $this->CI. what is it? Is it CI via get_instance(), or just something you thought would work? If the latter, use code from here to get to CI instance in library

CI object from library

- also, I'm not sure whether 'core' controller class is initiaded (maybe only extended), so you have to initiate it.

- I'm not sure whethere you can call underscored functions from outside Smile

Adam
#3

[eluser]Bramme[/eluser]
1. $this->CI is indeed a variable filled with $this->CI =& get_instance()
2. When I $this->CI->load->library('MY_Controller') (or CI_Controller or Controller) I get a "Unable to load the requested class: my_controller", which seems logical as you otherwise never have to load it.
3. Scrapping the underscore didn't change anything.
#4

[eluser]Adam K[/eluser]
Actually, I was thinking, why call it from core class?

I'm bit lost here about your files structure, but:
a. If you're calling the underscore function from controller, why not do $this->_function or (if it's from diferent controller) $this->that_loaded_controller->_function()
b. If it's called from library, I think there must be a way to run that function in original caller Controller:

controllerA->load library->call function X with parameter $this
in functionX use $param->_function()

Or, am I that wrong (I haven't worked with CI longer time)
#5

[eluser]Bramme[/eluser]
I have update the initial post a bit, I hope this makes things more clear.
#6

[eluser]Bramme[/eluser]
I'm going to bump this, because I really need it solved.

Let me simplify my question: can I access controller methods in a library?




Theme © iAndrew 2016 - Forum software by © MyBB