Welcome Guest, Not a member yet? Register   Sign In
Any way to access the controller (session) from the model?
#1

[eluser]madwhistler[/eluser]
Ok, I'm probably violating some sort of scoping morality rules here but...

I need access to $this->session->userdata() from a Model object. Inside the model, $this->session doesn't work. Is there *any* way to get hold of my controller's handle??

Reasons for doing this: I'm using that scary non-supported htmlform extension (because it's so very cool) and I need to filter the auto-generated dropdown lists. There's a callback for that: get_htmlform_list(), but it needs to be in the model class. Unfortunately, for my get_htmlform_list() to filter things right, it needs to grab some session userdata variables.

I'm bollixed here...can anyone help?
#2

[eluser]jalalski[/eluser]
Pass the session to the Model?

class MyModel_model extends Model {
...
function doSomething( $data ) {
}
};

and in the controller:
$this->load->Model('MyModel_model', 'MyModel');
$this->MyModel->doSomething( $this->session->userdata() );

approximately speaking...

jal
#3

[eluser]madwhistler[/eluser]
Sadly, that won't work. get_htmlform_list() is a CALLBACK from the $modelobject->render_form() function, which in turn is called from within a view.

Because render_form is calling my model directly, I don't have the opportunity to pass in the session object. Nor does there seem to be a way to pass it through render_form(), unless I'm missing something...

Thanks though. Any other suggestions?
#4

[eluser]WanWizard[/eluser]
Libraries should be available in all models, so if you can't access the session library, I wonder what's wrong. Do your models extend Model? And do you call the parent constructor (which is what assigns loaded libraries to models)?

Also, you shouldn't use CamelCase in your class names (which is discussed in the user guide), CI favours lowercase file and classnames, a first capital is ok for class names (not for file names!).
#5

[eluser]Codepeak[/eluser]
Accessing the scope from a model isn't hard nor impossible. You probably just forgot to do parent::Model();. Check the code below:

class Mymodel extends Model {

function Mymodel()
{
parent::Model();
}

function get_entries_from_db()
{
echo $this->session->userdata('xxx');
}

}
#6

[eluser]madwhistler[/eluser]
Ah, but being a DataMapper (DMZ) kinda guy, my models have been extending DataMapper, not Model or CI_Model, using the PHP5 constructor as:

Code:
function __construct($id = NULL)
    {
        parent::__construct($id = NULL);
    }

I have assumed, in my naive haze of ignorance, that this would make my $this be the $this I so desperately need. Have I been led so far astray? Must I beg DataMapper to speak to my session?

If anyone can enlighten me and keep me from digging into the bowels of DataMapper, it would be so very appreciated...
#7

[eluser]WanWizard[/eluser]
You should have mentioned that, that changes things.

In Datamapper models you have to access CI through the instance.
Code:
$CI =& get_instance();

echo $CI->session->userdata('blah');
#8

[eluser]madwhistler[/eluser]
Sorry, I thought the htmlform stuff was sufficient clue, but I had my CI brain on not my DMZ brain.

Nonetheless, you are my Official Hero of the Week since the $ci =& get_instance(); worked like a charm. All my models now nicely filter themselves before dropping down.

Kudos WW...




Theme © iAndrew 2016 - Forum software by © MyBB